【发布时间】:2015-04-29 13:58:12
【问题描述】:
我正在 Learn Python the Hard Way 并且正在进行练习 16。学习练习说要使用 read 和 argv 编写脚本。
我的代码如下:
from sys import argv
script, file_name, pet_name = argv
print "Ah, your pet's name is %r." %pet_name
print "This will write your pet's name in a text file."
print "First, this will delete the file. "
print "Proceeding..."
writefile = open(file_name, 'w')
writefile.truncate()
writefile.write(pet_name)
writefile.close
raw_input("Now it will read. Press ENTER to continue.")
readfile = open(file_name, "r")
print readfile.read()
代码一直有效。当它说要打印文件时,命令行给出一个空行。
PS C:\Users\[redacted]\lpthw> python ex16study.py pet.txt jumpy
Ah, your pet's name is 'jumpy'.
This will write your pet's name in a text file.
First, this will delete the file.
Proceeding...
Now it will read. Press ENTER to continue.
PS C:\Users\[redacted]\lpthw>
我不确定为什么脚本只是打印一个空白文件。
【问题讨论】:
标签: python python-2.7 powershell