import os
import csv
[docs]def get_directories():
"""Get a sorted list of directories in the current working directory."""
return sorted([d for d in os.listdir() if os.path.isdir(d)])
[docs]def parse_final_energy(file_path):
"""Extract the last 'FINAL SINGLE POINT ENERGY' from the specified file."""
last_energy = None
try:
with open(file_path, 'r') as file:
for line in file:
if line.startswith("FINAL SINGLE POINT ENERGY"):
last_energy = line.split()[4] # Update to the last occurrence
except FileNotFoundError:
print(f"File not found: {file_path}")
except IndexError:
print(f"Malformed line in file: {file_path}")
return last_energy
[docs]def write_to_csv(output_file, data):
"""Write energy data to a CSV file."""
with open(output_file, 'w', newline='') as csvfile:
fieldnames = ["Directory", "Energy"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(data)
if __name__ == "__main__":
extract()