以艰难的方式学习 Python — 在 运行 再次归档时练习 15 错误

Learn Python the hard way — exercise 15 error while running file again

我正在尝试 运行 此代码

from sys import argv

script, filename = argv

txt = open(filename)

print(f"Here's your file {filename}:")
print(txt.read())

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

txt_again = open(file_again)

print(txt_again.read())

此代码要求我再次输入文件名,但当我输入时,却出现错误。附上错误图片。有人可以告诉我做错了什么吗?

error message

Python 从用户的当前目录中获取文件名,而不是脚本。因此,如果您从 C:/Users/prince.bhatia 启动脚本,它实际上会查找文件 C:/Users/prince.bhatia/ex15_sample.txt

您正在执行下一个目录中的代码。 转到 Powershell 提示并将目录更改为您保存 files.and 运行 脚本的目录。

或阅读此文档以了解绝对路径规范:

https://automatetheboringstuff.com/chapter8/