读取文件时出现Python 3无效语法错误

时间:2020-01-11 15:40:30

标签: python python-3.x

我正在尝试学习python 3,这本书中有一项练习,要求我们在调用脚本时在命令行上提供文件名。但是,我遇到语法错误,无法弄清楚自己在做什么错。

环境:运行Python 3.8.0 x64位的Windows 10笔记本电脑

脚本:

from sys import argv

script, filename = argv

txt = open(filename)

print("Here's your file %r" % filename)
print(txt.read())

print("Type the filename again: ")
file_again = input("> ")

txt_again = open(file_again)

print txt_again.read()

命令行输入:

PS E:\~Python Scripts> python .\ex15.py ex15_sample.txt  

错误:

    PS E:\~Python Scripts> python .\ex15.py ex15_sample.txt                                                                   File ".\ex15.py", line 15
    print txt_again.read()
          ^
SyntaxError: invalid syntax

谢谢!

0 个答案:

没有答案