0

I had several print() statements in my code. I want run the code overnight and have them write to file. I tried redirecting output to file as:

python mycode.py >> log.txt

While python process is running, I cannot see anything written to file. When I kill the process and open the log file, I find the output in it.

Similar behavior was seen when I explicitlys specified file handle in the every print statement:

output_file = open('log.txt','w')
print('blablabla', file=output_file)

How can I make print to write the file immediately? Am I missing anything?

Rnj
  • 1,067
  • 1
  • 8
  • 23

2 Answers2

0

You are not seeing your changes right away because in some cases, due to buffering, changes made to a file may not show until you close the file.

To save changes without closing, please, refer to this page Can I save a text file in python without closing it?

go2nirvana
  • 1,598
  • 11
  • 20
  • Does it mean I have to call `output_file.flush()` after every `print()` call? – Rnj Nov 09 '20 at 09:42
  • Not necessary after **every** print. Changes are accumulating and you only need to `flush` it when you want it. – go2nirvana Nov 09 '20 at 09:44
  • Yess thats correct, that solves problem to some extend. Seems thats how all python file APIs behave, right? Every time we flush or close, its written to disk. What about `os.fsync(file.fileno())`? – Rnj Nov 09 '20 at 09:52
0

Can you try this and tell me if it updates, immediately or not. I'm always using this syntax in PyCharm. I hope it works with u, too.

If you want to insert bunch of data one at time.

with open("log.txt", 'w') as test:
    print ("hello world!", file=test)

If you want to insert data into .txt, then do some process, then back again to add more data to the .txt

test = open("log.txt", 'w')
print ("hello world!", file=test)
#==== Do some processes ====
print("This's the end of the file")
test.close()
Ahmed
  • 796
  • 1
  • 5
  • 16
  • This will open-close file every time – go2nirvana Nov 09 '20 at 09:45
  • This doesnt solve my problem write? – Rnj Nov 09 '20 at 09:47
  • This will close after you finish editing because in the syntax you wrote, you don't have a close clause at the end of it, so that the file predicts from you to write more until you done the complaining. `with open` is better because it gives you the ability to write the bulk of code at a time executed at time, too. If you want to initialized your `open` function at do some xode then back to adding something else. You've to add `close()` at the end. – Ahmed Nov 09 '20 at 09:52
  • I will attach what i explained into my answer. – Ahmed Nov 09 '20 at 09:58