简体   繁体   English

尝试通过 VSCodium 打开执行 Python 脚本的文件时没有此类文件或目录错误

[英]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.

相关问题 尝试从 python 中的另一个目录打开文件时显示 FileNotFound 错误 - Showing FileNotFound Error when trying to open file from another directory in python 试图打开与我的 Python 脚本位于同一目录中的 csv 文件,但得到的错误 2 文件不存在? - Trying to open a csv file that lives in the same directory as my Python script, but getting error2 file doesn't exist? 尝试打开文本文件时,Python脚本抛出非常奇怪的错误 - Python script throwing very odd error when trying to open a text file Python,无法打开文件,错误 [2] 没有这样的文件或目录 - Python, unable to open file, Error [2] no such file or directory 我正在尝试通过 python 打开文件 - I'm trying to open a file via python 无法在Python中打开文件“无此文件或目录”错误 - Cannot open file in Python “No such file or directory” error 出现错误:FileNotFoundError: [Errno 2] No such file or directory when using Python open() - Getting error: FileNotFoundError: [Errno 2] No such file or directory when using Python open() Python:尝试在目录中写入或覆盖文本文件时出错 - Python: error when trying to write or overwrite a text file in a directory 尝试通过 Python 3 命令 Shell 打开文件时出现语法错误 - Syntax error when trying to open a file through Python 3 Command Shell 尝试在 python 中打开文件进行读取(带有特殊字符)时出错 - Error when trying to open a file for reading (with special character) in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM