简体   繁体   English

为python写输出不起作用

[英]writing output for python not functioning

I am attempting to output a new txt file but it come up blank. 我正在尝试输出一个新的txt文件,但出现空白。 I am doing this 我正在做这个

my_file = open("something.txt","w")
#and then
my_file.write("hello")

Right after this line it just says 5 and then no text comes up in the file 在此行之后,它只说5,然后文件中没有文本

What am I doing wrong? 我究竟做错了什么?

You must close the file before the write is flushed. 您必须在刷新写入之前关闭文件 If I open an interpreter and then enter: 如果我打开翻译,然后输入:

my_file = open('something.txt', 'w')
my_file.write('hello')

and then open the file in a text program, there is no text. 然后在文本程序中打开文件,没有文本。

If I then issue: 如果再发出:

my_file.close()

Voila! 瞧! Text! 文本!

If you just want to flush once and keep writing, you can do that too: 如果您只想刷新一次并继续写入,也可以这样做:

my_file.flush()
my_file.write('\nhello again')  # file still says 'hello'
my_file.flush()   # now it says 'hello again' on the next line

By the way, if you happen to read the beautiful, wonderful documentation for file.write , which is only 2 lines long, you would have your answer (emphasis mine): 顺便说一句,如果您碰巧读了 file.write 精美文档 ,它只有两行,那么您将得到答案(强调我的意思):

Write a string to the file. 将字符串写入文件。 There is no return value. 没有返回值。 Due to buffering, the string may not actually show up in the file until the flush() or close() method is called. 由于存在缓冲,在调用flush()或close()方法之前,字符串实际上可能不会显示在文件中。

If you don't want to care about closing file, use with : 如果您不想关闭文件,请with使用:

with open("something.txt","w") as f: 
    f.write('hello')

Then python will take care of closing the file for you automatically. 然后python会自动为您关闭文件。

As Two-Bit Alchemist pointed out, the file has to be closed. 正如两位炼金术士指出的,该文件必须关闭。 The python file writer uses a buffer ( BufferedIOBase I think), meaning it collects a certain number of bytes before writing them to disk in bulk. python文件编写器使用缓冲区(我认为是BufferedIOBase ),这意味着在将它们批量写入磁盘之前,它会收集一定数量的字节。 This is done to save overhead when a lot of write operations are performed on a single file. 这样做是为了在单个文件上执行大量写操作时节省开销。

Also: When working with files, try using a with -environment to make sure your file is closed after you are done writing/reading: 另外:处理文件时,请尝试使用with -environment的文件,以确保在完成写入/读取后关闭文件:

with open("somefile.txt", "w") as myfile:
    myfile.write("42")

# when you reach this point, i.e. leave the with-environment,
# the file is closed automatically.

The python file writer uses a buffer (BufferedIOBase I think), meaning it collects a certain number of bytes before writing them to disk in bulk. python文件编写器使用缓冲区(我认为是BufferedIOBase),这意味着在将它们批量写入磁盘之前,它会收集一定数量的字节。 This is done to save overhead when a lot of write operations are performed on a single file. 这样做是为了在单个文件上执行大量写操作时节省开销。 Ref @m00am 引用@ m00am

Your code is also okk. 您的代码也没问题。 Just add a statement for close file, then work correctly. 只需添加一个用于关闭文件的语句,然后即可正常工作。

my_file = open("fin.txt","w")
#and then
my_file.write("hello")
my_file.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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