我在运行代码时收到此消息:
Python 3.py 3.txt
I'm going to open the your file SMARTIE boy!
If you don't want that - press CONTROL_C-C
If you do want that- Press RETURN
Now i'm actually gonna do it
Now we're gonna truncate it
Write something for your beautiful day.
Traceback (most recent call last):
File "3.py", line 19, in <module>
line1 = raw_input("wefew")
TypeError: 'str' object is not callable
我不知道为什么
from sys import argv
script, filename = argv
print "I'm going to open the your file SMARTIE boy!"
print "If you don't want that - press CONTROL_C-C"
print "If you do want that- Press RETURN"
raw_input = ("<3")
print "Now i'm actually gonna do it"
mission = open(filename, 'w')
print "Now we're gonna truncate it"
mission.truncate()
print "Write something for your beautiful day."
line1 = raw_input("wefew")
line2 = raw_input("fwefw")
mission.write(line1)
print "Well done! let's close the file now"
mission.close()
另一个问题 - 代码运行良好后如何打印我更改的txt文件?谢谢 :)
你在这里影响raw_input
功能:raw_input = ("<3")
。这就是为什么你以后不能使用原始功能的原因。你可能只想调用这个函数:
raw_input("<3")
另一个问题 - 代码运行良好后如何打印我更改的txt文件?
再次打开文件,阅读并打印内容。
建议:尝试阅读有关处理打开和关闭文件对象的context manager概念:
with open(filename) as textfile:
print(textfile.read())