2

I'm very new to programming, and to vscode.

I'm learning Python and currently I am learning about working with files.

The path looks like this: /home/anewuser/learning/chapter10.

The problem: completely basic "read file in python" lesson does not work in vscode because no such file or directory error raises when running my .py file, located in ~/learning/chapter10. But vscode wants that my .txt file I am supposed to open in python, to be in ~/learning directory, then it works. I don't like this behaviour.

All I want is to be able to read file placed in the directory where the .py file is. How to do this?

5
  • Welcome to SO. Please check how to ask a question. To help people understand your problem, you would need to post some code and highlight, if possible, which part of your code is causing the problem. In your case, it sounds like a folder issue. How did you use the Python built-in open function? Commented Mar 15, 2019 at 22:32
  • @Al-un It is the most basic code to read a file: with open('pi_digits.txt') as file_object: contents = file_object.read() print(contents) Error: Traceback (most recent call last): File "/home/anewuser/learning/chapter10/file_reader.py", line 1, in <module> with open('pi_digits.txt') as file_object: FileNotFoundError: [Errno 2] No such file or directory: 'pi_digits.txt' When i put both .py and pi_digits.txt in ~/learning/chapter10, this error happens. When I put 'pi_digits.txt' in ~/learning directory, it works. What to do make it work in the same directory? Thanks! Commented Mar 16, 2019 at 16:20
  • Also, this code works fine when run directly in a terminal. No errors. Other IDE like KDevelop also has no problem with it, but it does not work in vscode. Commented Mar 16, 2019 at 16:22
  • 1
    Please post the code in your question, you can edit it ^^ Commented Mar 17, 2019 at 9:07
  • As for your question itself, you may have a look at stackoverflow.com/questions/7165749/… Commented Mar 17, 2019 at 16:40

2 Answers 2

1

Because in your case ~/learning is the default cwd (current working directory), VSCode looks for pi_digits.txt in that location. If you put pi_digits.txt beside file_reader.py (which is located at ~/learning/chapter10), you'll have to specify the path (by prepending chapter10/ to the .txt file).

So you should do this:

with open('chapter10/pi_digits.txt') as file_object:
    contents = file_object.read()
    print(contents)

If you want to change the default current working directory (for example you want to change it to ~/learning/chapter10), you'll have to do the following:

~/learning/chapter10/file_reader.py

import os  # first you need to import the module 'os'

# set the cwd to 'chapter10'
os.chdir('chapter10')

# now 'file_reader.py' and 'pi_digits.txt' are both in the cwd
with open('pi_digits.txt') as file_object:
    contents = file_object.read()
    print(contents)

With os.chdir('chapter10') you've set chapter10 as the default cwd, in which VSCode now will look for pi_digits.txt.

For detailed information about os.chdir() you can read through the official documentation or take a look at this post on stackoverflow.

Sign up to request clarification or add additional context in comments.

Comments

1

In "User Settings", use the search bar to look for "python.terminal.executeInFileDir" and set (=) its value to "true" instead of "false".

I took this answer from here How to run python interactive in current file's directory in Visual Studio Code? this is my first time putting an answer on StackOverflow so I apologize if I didn't do it the right way

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.