![]() |
write code that resides in parent directory - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: write code that resides in parent directory (/thread-41860.html) |
write code that resides in parent directory - franklin97355 - Mar-29-2024 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') RE: write code that resides in parent directory - Larz60+ - Mar-30-2024 The following will get the absolute paths. Note that root_dir will get path to the file, including the python file name. If you just wanted the current path, use root_dir = Path('.') You would also have to assure that the current path is the the same as __file__ path. You can still use __file__. but to get previous directory,would need parent_dir = root_dir.parent.parent.absolute() root_dir = Path(__file__).absolute() print(root_dir) parent_dir = root_dir.parent.absolute() print(parent_dir) RE: write code that resides in parent directory - Gribouillis - Mar-30-2024 (Mar-29-2024, 12:03 AM)franklin97355 Wrote: Any insights are welcome.I would rather use the builtin argparse module to handle the arguments instead of having user interaction. For example you could invoke Among other benefits, this allows you to invoke the program in a makefile for example. Here is a possible versionfrom argparse import ArgumentParser from pathlib import Path import sys TARGET_LINE = "SUMMARY:" DATE_LINE = "DTSTART;VALUE=DATE:" parser = ArgumentParser(description='Convert .ics file into .csv') parser.add_argument( "-o", "--outfile", dest="outfile", default="", action="store", required=False, help="Destination CSV file, defaults to stdout", metavar="OUTFILE", ) parser.add_argument( "-m", "--month", dest="month", action="store", required=True, help="Month in format YYYYMM", metavar="YYYYMM", ) parser.add_argument(dest="infile", help="Source ICS file", metavar="INFILE") def process_lines(lines, date_range, fp): for line_num, line in enumerate(lines, 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") def main(): args = parser.parse_args() print(args) if not args.infile.endswith(".ics"): args.infile = args.infile + ".ics" infile = Path(args.infile) if not infile.is_file(): print(f"Error: ICS file not found: {args.infile}", file=sys.stderr) return 1 lines = infile.read_text().strip().splitlines() if args.outfile: if not args.outfile.endswith(".csv"): args.outfile = args.outfile + ".csv" with open(args.outfile, "w") as ofp: process_lines(lines, args.month, ofp) print(f"Output written to: {args.outfile}") else: process_lines(lines, args.month, sys.stdout) return 0 if __name__ == "__main__": sys.exit(main())Here is the help automatically generated by this program
RE: write code that resides in parent directory - franklin97355 - Apr-14-2024 Thank you, I was remiss in not checking my forum posts. Now I will check your suggestions and see if I can make heads or tails out of that. Again thanks to you two. |