Just to clarify:
In the original code (posted below again), parts was used to create the output file name, and had nothing to do with input file name (as requested by member). There are several ways to do this, I chose splitting into parts, adding in 'Modified' to end of filename, and joining back together.
Original code:
In the original code (posted below again), parts was used to create the output file name, and had nothing to do with input file name (as requested by member). There are several ways to do this, I chose splitting into parts, adding in 'Modified' to end of filename, and joining back together.
Original code:
import csv from pathlib import Path from tkinter.filedialog import askopenfilename import sys def read_csv_file(filename): parts = list(filename.parts) parts[-1] = f"{filename.stem}Modified{filename.suffix}" parts[0] = '' outfilename = Path(f"{'/'.join(parts)}") print(f"new output file name: {outfilename}") with filename.open() as fp, outfilename.open('w') as fout: crdr = csv.reader(fp, delimiter=',') cwrtr = csv.writer(fout, delimiter=',') for row in crdr: print(row) # Modify row as desired here cwrtr.writerow(row) def get_filename(): badcount = 0 while(True): try: if badcount > 2: print("Three strikes and your out!") sys.exit(-1) filename = Path(askopenfilename(filetypes=[("CSV files","*.csv")])) break except TypeError: badcount += 1 print(f"bad filename, try again") return filename if __name__ == '__main__': read_csv_file(get_filename())Please note f-strings are not being properly displayed. items within brackets should be highlighted as they are placeholders for the variable they contain.