(Aug-06-2021, 10:00 PM)AlphaInc Wrote: This works fine except for one last thing - how can I save the printed output to the same file and overwrite it?
In your code you read the data from the file into a variable with <file>.read(). You can do the opposite (on a file that has been opened for writing) with <file>.write().
# read in filedata with open('text.txt', 'r') as file : filedata = file.read() # do stuff with filedata here # write filedata out here (overwriting the file) with open('text.txt', 'w') as file: file.write(filedata)This is a bit dangerous because for a period of time, there is no data on disk. For a production script, I'd change this to write the file to another name, then after the write has succeeded, rename the file.