Mar-29-2024, 12:03 AM
New to Python and have a question on usage. I am using Linux. I have a Python 3.11 program that reads, parses, and writes an ics file that works if it is in the same directory as the files. I would like to have it not have to be in the same directory but in the parent directory. Any insights are welcome.
from pathlib import Path ROOT_DIR = Path(__file__).parent ics_file = input('\n ics file name?') if('.ics'not in ics_file): ics_file = ics_file + ".ics" output_file = input('\n csv file name?') if('.csv'not in output_file): output_file = output_file + ".csv" date_range = input('\n date range (YYYYMM)') INPUT_FILE = ROOT_DIR / ics_file OUTPUT_FILE = ROOT_DIR / output_file print(ROOT_DIR) print(INPUT_FILE) print(OUTPUT_FILE) print(ROOT_DIR.parent) if not INPUT_FILE.exists(): print("ICS file not found") TARGET_LINE = 'SUMMARY:' DATE_LINE = 'DTSTART;VALUE=DATE:' fp = open(OUTPUT_FILE,"w") output_lines = [] contents = INPUT_FILE.read_text().strip().splitlines() for line_num, line in enumerate(contents, 1): if line.startswith(DATE_LINE): y = line.strip(DATE_LINE) elif line.startswith(TARGET_LINE): x = line.strip(TARGET_LINE) if(date_range in y): fp.write(str(y)+", ") fp.write(x) fp.write("\n") print('DONE')