我的代码基本上将用户输入并将其放入文件中。当我运行它时,代码会创建文件,但始终为空。你能帮我吗?
#Asks file name from user.
FileName = input("Please provide a name for your file: (no spaces) ")
FileFullName = FileName + ".txt"
TypeInput = input("What would you like to type? ")
FileLine = TypeInput + "n"
#Opens file & puts user input into file.
saveFile = open(FileFullName, 'a')
saveFile.write(FileLine)
print("All finished!")
saveFile.close()
使用with open
,因此您不需要关闭文件。它是这样的工作:
#Asks file name from user.
FileName = input("Please provide a name for your file: (no spaces) ")
FileFullName = FileName + ".txt"
TypeInput = input("What would you like to type? ")
FileLine = TypeInput + "n"
#Opens file & puts user input into file.
with open(FileFullName, 'a') as saveFile:
saveFile.write(FileLine)
print("All finished!")