简体   繁体   中英

How to keep a file object in python open and keep writing to it without having to call file.close()

This is a really basic question but I want to know if its possible to open a file and keep writing to it while the actual file gets update in real time.

Basically I want to be able to do this and have 'File' act kinda like sys.stdout where you don't have to close the file for the output to be visible.

File = open("File.txt", "w")
File.write("Hello")

All you have to do is use the flush function:

File = open("File.txt", "w")
File.write("Hello")
File.flush()

This will have the output written to the file without closing the connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM