0

I have a simple program that writes to a file in a for loop (30k+ iterations). I noticed that the program is taking abnormally long to finish, after which I decided to kill it. The file it is writing to has 29900 lines created withing 30s and the remaining 100 are not written - the program doesn't exit and it takes hours to do so.

Once I killed it, it finished writing the remaining 100 or so lines.

f1 = open('parts.txt', "w") for line in lines: category = line[2] f1.write(category + '\n') print('------- done -------') f1.close() 

Is there anything obvious that I am missing?

2
  • 1
    Error - you have unmatched " and ' and ) - so this is not your real code. Please fix and give us a correct minimal reproducible example. Thanks Commented Oct 17, 2019 at 13:31
  • It is not my real code, because the file parts.txt is too large to post here. Rerunning and reproducing the code here is not necessary - my usage of files may be wrong. Commented Oct 17, 2019 at 13:35

2 Answers 2

1

Use a with statement, this way the file is always closed independent of the exit status.

with open('parts.txt', 'w') as fw: for line in lines: category = line[2] fw.write(category + '\n') print('----- done ------') 
Sign up to request clarification or add additional context in comments.

5 Comments

The explicit fw.close() is not needed?
while a sound advice this answer is premature - the only error the OP code shows is SyntaxError: EOL while scanning string literal because it is not valid python.
@oneiros nope, this is called implicitly when the with block is exited.
@oneiros look up the with statement context manager
@Krrr - will do. Thank for the post - I am somewhat new to the language and this is a concept I need to understand.
1

I spent a while trying to figure out why this was happening to me - it turns out it wasn't my Python code's fault at all for me: my IDE (PyCharm) doesn't seem to refresh (or at least often) the file tree whilst the program is running. However, the files were still generated, and looking at the file explorer/finder (or unfocusing/refocusing PyCharm), the files were generated.

I know this wasn't the answer for OP, but for anyone else who's having issues down the line (and searching around and comes across this) and nothing done in Python seems to work - check if the file has been generated with your system's file browser!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.