Here's some code that will open a csv file, modify the filename and write back to new file name that is the same as the input, and in the same directory, byt with 'Modified' added to filename.
there is a comment where you can insert code to modify the input record before writing back out.
Try it out:
there is a comment where you can insert code to modify the input record before writing back out.
Try it out:
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())