question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Errno 2] No such file or directory (Python)

See original GitHub issue

Expected behaviour

Run a program that reads a file stored in the same directory as the program.

Actual behaviour

VS Code is returning the following in the terminal:

Traceback (most recent call last):
File "/Filepath/10-1_learning_python.py", line 3, in <module>
with open(filename) as file_content:
FileNotFoundError: [Errno 2] No such file or directory: 'learning_python.txt'

Steps to reproduce:

I am trying to run a very simple Python program in VS Code. In the same subfolder I have the two following files:

  • 10-1_learning_python.py
  • learning_python.txt

This is the code in “10-1_learning_python.py”:

filename = 'learning_python.txt'

with open(filename) as file_content:
    content = file_content.read()
    print(content)

When running the code I get this error:

FileNotFoundError: [Errno 2] No such file or directory: ‘learning_python.txt’

This code works (using the very same directory and files) if I run it in other applications such as SublimeText.

Environment data

I am using macOS Catalina 10.15.5. My VS Code version is as follows:

Version: 1.45.1 Commit: 5763d909d5f12fe19f215cbfdd29a91c0fa9208a Date: 2020-05-14T08:33:47.663Z Electron: 7.2.4 Chrome: 78.0.3904.130 Node.js: 12.8.1 V8: 7.8.279.23-electron.0 OS: Darwin x64 19.5.0

Value of the python.languageServer setting: Microsoft

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

40reactions
karthiknadigcommented, Jun 8, 2020

You might have to enable, “Execute in file dir”. image

3reactions
denisclccommented, Jun 9, 2020

That did it! I did not know about this setting.

Thank you very much and sorry for the trouble…

Read more comments on GitHub >

github_iconTop Results From Across the Web

FileNotFoundError: [Errno 2] No such file or directory [duplicate]
I am trying to open a CSV file but for some reason python cannot locate it. Here is my code (it's just a...
Read more >
Python FileNotFoundError: [Errno 2] No such file or directory ...
The Python FileNotFoundError: [Errno 2] No such file or directory error is often raised by the os library. This error tells you that...
Read more >
FileNotFoundError: [Errno 2] No such ... - Net-Informations.Com
The error "FileNotFoundError: [Errno 2] No such file or directory" is telling you that there is no file of that name in the...
Read more >
How to fix FileNotFoundError Errno 2 no such file or directory
The error FileNotFoundError Errno 2 no such file or directory occurs when Python cannot find the specified file in the current directory.
Read more >
Filenotfounderror errno 2 no such file or directory python error ...
How to solve filenotfounderror errno 2 no such file or directory in python is shown.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found