I have a file called fName.txt in a directory. Running the following python snippet (coming from a simulation) would add 6 numbers into 3 rows and 2 columns into the text file through executing the loop (containing the snippet) three times. However, I would like to empty the file completely before writing new data into it. (Otherwise running the script for many times would produce more than three rows needed for the simulation which would produce nonsense results; in other words the script needs to see only three rows just produced from the simulation). I have come across the following page how to delete only the contents of file in python where it explains how to do it but I am not able to implement it into my example. In particular, after pass
statement, I am not sure of the order of statements given the fact that my file is closed to begin with and that it must be closed again once print
statement is executed. Each time, I was receiving a different error message which I could not avoid in any case. Here is one sort of error I was receiving which indicated that the content is deleted (mostly likely after print statement):
/usr/lib/python3.4/site-packages/numpy/lib/npyio.py:1385: UserWarning: genfromtxt: Empty input file: "output/Images/MW_Size/covering_fractions.txt" warnings.warn('genfromtxt: Empty input file: "%s"' % fname) Traceback (most recent call last): File "Collector2.py", line 81, in LLSs, DLAs = np.genfromtxt(r'output/Images/MW_Size/covering_fractions.txt', comments='#', usecols = (0,1), unpack=True) ValueError: need more than 0 values to unpack
This is why I decided to leave the snippet in its simplest form without using any one of those suggestions in that page:
covering_fraction_data = "output/Images/MW_Size/covering_fractions.txt"
with open(covering_fraction_data,"mode") as fName:
print('{:.2e} {:.2e}'.format(lls_number/grid_number, dla_number/grid_number), file=fName)
fName.close()
Each run of the simulation produces 3 rows that should be printed into the file. When mode
is 'a', the three produced lines are added to the existing file producing a text file that would contain more than three rows because it already included some contents. After changing 'a' to 'w', instead of having 3 rows printed in the text file, there is only 1 row printed; the first two rows are deleted unwantedly.
Workaround:
The only way around to avoid all this is to choose the 'a' mode
and manually delete the contents of the file before running the code. This way, only three rows are produced in the text file after running the code which is what is expected from the output.
Question:
So, my question is basically, "how can I modify the above code to actually do the deletion of the file automatically and before it is filled with three new rows?"
Your help is greatly appreciated.