从python脚本打开并读取.txt文件

从python脚本打开并读取.txt文件,python,Python,我已经使用了这个代码,它显示的错误,如图片中所附 我该怎么办 您正在尝试执行一个.txt文件。您应该像这样运行脚本: from sys import argv script,filename=argv txt=open(filename) print("here's your file %r:\n" % filename) print txt.read() print("type the filename again:") file_again=input("@") txt_again=ope

我已经使用了这个代码,它显示的错误,如图片中所附

我该怎么办


您正在尝试执行一个
.txt
文件。您应该像这样运行脚本:

from sys import argv
script,filename=argv
txt=open(filename)
print("here's your file %r:\n" % filename)
print txt.read()

print("type the filename again:")
file_again=input("@")
txt_again=open(file_again)
print txt_again.read()

其中
my_script.py
应该包含您的代码。

argv可能是您案例中的一个列表。请将您遇到的错误放在图像的位置?
python.exe
需要一个脚本(.py文件),而不是您想要读取的文本文件。@bigbounty我没有将.py脚本放在cmd中,这会弹出错误。我得到了答案。谢谢:)
C:\path\to\python.exe my_script.py path\to\your\file.txt