python - 根据用户输入创建文件

标签 python file

任何人都可以帮助我完成这项任务。我是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/

相关文章:

android - 如何使用JNI在Android中读取/写入文件slacks?

android - 在android中显示文件列表?

python - 如何在有限的环境中从 Python 中的大文件中删除行

python - 使用命令行工具组合多个图像

python - 替换多索引中除第一行以外的所有内容

Python3 ginput 警告 : Using default event loop

python - 我可以从多个进程/线程写入 HDF5 文件吗?

java - 将数据保存到托管 .dat 文件

c++ - 使用 C++ 删除文本文件中重复行的内存有效方法

python - 无法取消 pickle 对象