3

I have a file called fName.txt in a directory. Running the following Python snippet 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 <module>
    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:

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?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Rebel
  • 472
  • 8
  • 25

2 Answers2

6

You are using 'append' mode ('a') to open your file. When this mode is specified, new text is appended to the existing file content. You're looking for the 'write' mode, that is open(filename, 'w'). This will override the file contents every time you open it.

rubik
  • 8,814
  • 9
  • 58
  • 88
  • sorry, that was 'w' in the original snippet and I changed it few times and ended up being 'a'. But still with that 'w' is not working. – Rebel Oct 12 '16 at 23:49
  • @Allan I don't understand. You say that there were three runs and you see only the result from the last one. But in the code you provided you only print to the file once, so for every run the content of the file is overridden. If you are indeed writing to the file three times, then remember that with the 'w' mode the file is overridden **when you open it**. That means that if you want three result per program run you have to open it just once and then keep writing in it, or opening it the first time with 'w' and the other times with 'a'. – rubik Oct 13 '16 at 13:09
  • Sorry for the misunderstanding. The code produces three consecutive rows for each run. I should have not used the word "running three times". So, yes in fact I need three results per run and I need to open it just once and then keep writing in it until all three rows are filled and then close the file. – Rebel Oct 13 '16 at 19:35
0

Using mode 'w' is able to delete the content of the file and overwrite the file but prevents the loop containing the above snippet from printing two more times to produce two more rows of data. In other words, using 'w' mode is not compatible with the code I have given the fact that it is supposed to print into the file three times (since the loop containing this snippet is executing three times). For this reason, I had to empty the file through the following command line inside the main.py code:

os.system("> output/Images/MW_Size/covering_fractions.txt")

And only then use the 'a' mode in the code snippet mentioned above. This way the loop is executing AND printing into the empty file three times as expected without deleting the first two rows.

Rebel
  • 472
  • 8
  • 25