任何人都可以帮助我完成这项任务。我是Python新手。我正在努力实现这一目标: 文件名应该是硬编码名称,称为:Server_Information.txt,并且第二列应该由用户输入插入,但日期戳。 builder :约翰·多伊 构建日期:%d%m%y build 原因: Playground 请求者:约翰·多伊
也许我可以使用这个测试脚本,但第一列不会显示在最终的测试文件中。
感谢任何有帮助的人
from sys import argv
script, filename = argv
print "We're going to erase %r." % filename
print "If you don't want that, hit CTRL-C (^C)."
print "If you do want that, hit RETURN."
raw_input("?")
print "Opening the file..."
target = open(filename, 'w')
print "Truncating the file. Goodbye!"
target.truncate()
print "Now I'm going to ask you for three lines."
line1 = raw_input("Built By : ")
line2 = raw_input("Build Date: %d%m%y ")
line3 = raw_input("Build Reason: ")
line4 = raw_input("Requestor: ")
print "I'm going to write these to the file."
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
target.write(line4)
print "And finally, we close it."
target.close()
最佳答案
尝试在 truncate() 后关闭并重新打开文件
target.close()
target = open(filename, 'w')
# ask for user input here
# and close file
关于python - 根据用户输入创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26292349/