2015-09-30 65 views
-1
import sys 

fileobject=open('file.txt','w') 
fileobject.write(sys.stdin.readline()) 
Cat 

在上面的代码中,执行后不应该在文件中吗?但是,当我运行它时,我发现文件为空。如果我的代码有误,有人可以解释sys.stdin.read()sys.stdout.write()是如何工作和使用的吗?Python文件处理:标准输入输出

回答

1

您需要关闭该文件

import sys 

fileobject = open('file.txt', 'w') 
fileobject.write(sys.stdin.readline()) 
Cat 
fileobject.close() 

如果你想看到关闭文件或退出程序,你可以使用刷新()之前更新的文件内容:

fileobject.flush() 

检查这标准输入/输出StackOverflow Question