raw_input()无法正确运行,因为语法

时间:2019-04-17 06:32:34

标签: python syntax

我在运行代码时收到此消息:

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文件? 谢谢:)

1 个答案:

答案 0 :(得分:0)

您在这里raw_input的功能是raw_input = ("<3")。这就是为什么以后无法使用原始功能的原因。您可能只想调用该函数:

raw_input("<3")
  

另一个问题-代码运行良好后,如何打印我更改过的txt文件?

再次打开文件,阅读并打印内容。

建议:尝试阅读有关context manager概念的知识,该概念为您处理打开和关闭文件对象:

with open(filename) as textfile:
    print(textfile.read())