0

I am currently in some truble regarding python and reading files. I have to open a file in a while loop and do some stuff with the values of the file. The results are written into a new file. This new file is then read in the next run of the while loop. But in this second run I get no values out of this file... Here is a code snippet, that hopefully clarifies what I mean.

while convergence == 0:
    run += 1
    prevrun = run-1

    if os.path.isfile("./Output/temp/EmissionMat%d.txt" %prevrun) == True:
        matfile = open("./Output/temp/EmissionMat%d.txt" %prevrun, "r") 
        EmissionMat = Aux_Functions.EmissionMat(matfile)
        matfile.close()
    else:
        matfile = open("./Input/EmissionMat.txt", "r") 
        EmissionMat = Aux_Functions.EmissionMat(matfile)
        matfile.close()

    # now some valid operations, which produce a matrix

    emissionmat_file = open("./output/temp/EmissionMat%d.txt" %run, "w")
    emissionmat_file.flush()
    emissionmat_file.write(str(matrix))

    emissionmat_file.close()

Solved it!

matfile.seek(0)

This resets the pointer to the begining of the file and allows me to read the file in the next run correctly.


7
  • why do you emissionmat_file.flush() before writing, and what is the content of matrix Commented Jun 6, 2013 at 8:38
  • What OS are you using? Commented Jun 6, 2013 at 8:41
  • @interjay I am using Win7. Commented Jun 6, 2013 at 8:46
  • @njzk2 I do emissionmat_file.flush() to get the content to my file otherwise i get no values in my file. The content of matrix is a numpy array with 40*40 values. But do not mind the write stuff. The resulting file looked as it is desired. I just simplified it here. Commented Jun 6, 2013 at 8:46
  • 1
    do you have a typo in your code? "./Output" and "./output" is not the same... pro tip: use a constant/variable for this... Commented Jun 6, 2013 at 8:49

3 Answers 3

1

Why to write to a file and then read it ? Moreover you use flush, so you are doing potentially long io. I would do

with open(originalpath) as f:
    mat = f.read()
while condition :
    run += 1
    write_mat_run(mat, run)
    mat = func(mat)

write_mat_run may be done in another thread. You should check io exceptions.

BTW this will probably solve your bug, or at least make it clear.

Sign up to request clarification or add additional context in comments.

5 Comments

This is part of an expectation maximization algorithm so i need the values from the previous run for the current run.
I solved it (see question post) I will post it as a "real" answer if the system allows me to. But thanks for your help!
With this pattern you use the previous run for the current one. It may be really more efficient because IO is a bottleneck and can kill you performances and less bug prone since you don't have to rely on the filesystem. Even if you solved your bug I suggest you to use this pattern.
Ok, I will try it. I honestly have not thought of the prefomance issue, so thanks.
Thanks a lot for this advice. I changed it the way you suggested it an it really improves the speed of the computation and also solves another error I got. :)
0

I can see nothing wrong with your code. The following concrete example worked on my Linux machine:

import os

run = 0
while run < 10:
    run += 1
    prevrun = run-1

    if os.path.isfile("output%d.txt" %prevrun):
        matfile = open("output%d.txt" %prevrun, "r")
        data = matfile.readlines()
        matfile.close()
    else:
        matfile = open("input.txt", "r")
        data = matfile.readlines()
        matfile.close()

    data = [ s[:-1] + "!\n" for s in data ]

    emissionmat_file = open("output%d.txt" %run, "w")
    emissionmat_file.writelines(data)
    emissionmat_file.close()

It adds an exclamation mark to each line in the file input.txt.

1 Comment

As you can see in the question post I solved the problem. I will put it in a "real" answer as soon as the system allows me to do this. Thanks for your help.
0

I solved it

before closing the file I do

matfile.seek(0)

This solved my problem. This methods sets the pointer of the reader to the beginning.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.