[英]No such file or directory error when trying to open a file executing a Python script via VSCodium
I've been using VSCodium lately and writing Python code with it.我最近一直在使用 VSCodium 并用它编写 Python 代码。 However, I can't seem to find a way to open an existing file when running the script with VSCodium.
但是,在使用 VSCodium 运行脚本时,我似乎找不到打开现有文件的方法。
This is the code I have, it's working perfectly fine when ran from a terminal, it works.这是我拥有的代码,从终端运行时它工作得很好,它工作正常。 But not with VSCodium.
但不是 VSCodium。
def remove_e_words(path,path_copy):
newfile = open(path_copy,"w")
orig = open(path,"r")
lines = orig.readlines()
for line in lines:
newfile.write(sans_e_spec(line)+"\n")
newfile.close()
orig.close()
Here's the error I have :这是我的错误:
FileNotFoundError: [Errno 2] No such file or directory: 'vers-queneau.txt'
I've searched for similar issues on stackoverflow, I've tried this: Can't run python code through VS Code.我在stackoverflow上搜索过类似的问题,我试过这个: Can't run python code through VS Code。 Can't open file ptvsd_launcher.py [Errno 22] Invalid Argument
无法打开文件 ptvsd_launcher.py [Errno 22] 无效参数
I have the downgraded version, it still doesn't work.我有降级版本,它仍然不起作用。
I also tried to use the file directory instead, changing a parameter in the Settings, it also didn't fix the issue.我还尝试改用文件目录,更改设置中的参数,但也没有解决问题。
What could I do to be able to solve this problem please?我该怎么做才能解决这个问题?
Thanks.谢谢。
You probably don't have the current working directory set to what you expect.您可能没有将当前工作目录设置为您所期望的。 If you run your code with
print(os.getcwd())
you will very likely find that is not the directory you expect it to be.如果您使用
print(os.getcwd())
运行您的代码,您很可能会发现它不是您期望的目录。 Either adjust your relative file path to take that cwd into account or specify the path in an absolute fashion (if the file is next to the file you're running you can calculate its location based on __file__
).调整您的相对文件路径以考虑该 cwd 或以绝对方式指定路径(如果该文件在您正在运行的文件旁边,您可以根据
__file__
计算其位置)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.