Varmushu
Varmushu

Reputation: 5

.write() and .writelines() returning a blank .txt file -- PYTHON

I have three string, numpy arrays (all of the same length) containing all the information I need.

I am trying to put the arrays together in a meaningful way in an empty text file I have defined as '1RESULTS.txt'

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Code

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

temp_str = ' '
temp_bool = False

for (a, b, c) in zip(np_sub1, np_sub2, np_sub3):

    with open('1RESULTS.txt', 'w') as f:

        temp_bool = False

        if a != temp_str:

            f.write('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
            f.write('*****' + a + '*****')
            '\n'
            f.write(b + '--' + c + ';')

            temp_str = a
            temp_bool = True

        elif (temp_bool == False) and (a == temp_str):
            f.write(b + '--' + c + ';')
            '\n'

print('Type Unknown: ' + str(counter))

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If I substitute 'f.write' with 'print' the output is as follows. This is what I would like 1RESULTS.txt to look like, but it remains blank.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*****42469000730000*****

17456638--Gamma;

2271876.--Resistivity;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*****42469000840000*****

4881574.--Resistivity;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*****42469000850000*****

4881576.--Resistivity;

Upvotes: 0

Views: 1429

Answers (3)

ducminh
ducminh

Reputation: 1352

f is opened and rewritten every iteration. Thus only the last iteration affects the content of the file. Change the 3rd and 4th lines to

with open('1RESULTS.txt', 'w') as f:
    (a, b, c) in zip(np_sub1, np_sub2, np_sub3):
       ...

and it should work as expected.

Upvotes: 1

Ishan Srivastava
Ishan Srivastava

Reputation: 1189

with open('1RESULTS.txt', 'w') as f:

here is your problem, you file is written over and over again each iteration deleting the previous entries. You should rather append to the file with

with open('1RESULTS.txt', 'a') as f:

EDIT: better use the code as follows instead of opening and closing a stream so many times

temp_str = ' '
temp_bool = False

with open('1RESULTS.txt', 'w') as f:

    for (a, b, c) in zip(np_sub1, np_sub2, np_sub3):

        temp_bool = False

        if a != temp_str:

            f.write('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
            f.write('*****' + a + '*****')
            '\n'
            f.write(b + '--' + c + ';')

            temp_str = a
            temp_bool = True

        elif (temp_bool == False) and (a == temp_str):
            f.write(b + '--' + c + ';')
            '\n'

    print('Type Unknown: ' + str(counter))

Upvotes: 1

Lelor
Lelor

Reputation: 46

The with statement manages the context of the open function, which processes the file for you. You shouldn't put it inside the loop because it will create a new context for each object of the iteration.

Since you're opening the file in the 'w' mode, it will overwrite anything you wrote in the previous iteration.

Upvotes: 0

Related Questions