运行 我在终端中的代码不起作用,因为找不到其中一个文件

Running my code in terminal doesn't work because in cannot find one of the files

我在 Pycharm 中创建了一个项目(测试),其中包含三个文件;一个包含我的 code/function (test1) 的文件,一个调用并执行函数的文件 (krypt.py),以及一个函数从中收集信息的 txt 文件 (data.txt)。当我尝试 运行 终端中的代码时,出现此错误:

(base) KKWs-MBP-2:~ Kristoffer$ /usr/local/bin/python3 "/Users/Kristoffer/Desktop/Pycharm prosjekter/test/krypt.py"
Nøkkel: 
Vil du lese eller legge til? (r/a) r
Traceback (most recent call last):
  File "/Users/Kristoffer/Desktop/Pycharm prosjekter/test/krypt.py", line 2, in <module>
    Hent()
  File "/Users/Kristoffer/Desktop/Pycharm prosjekter/test/test1.py", line 27, in Hent
    with open('data.txt', 'rb') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'data.txt'

我必须为终端提供什么才能读取 txt 文件?

data.txt 不在您执行此脚本的目录中。

从您的 PS1 来看,您似乎在主目录中,程序正在寻找 data.txt

尝试导航到包含 data.txt 文件的目录,然后 运行 使用相同的命令。或者在你写的程序中把路径设为绝对路径