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?

Joey
  • 1,436
  • 2
  • 19
  • 33
ANewUser
  • 21
  • 1
  • 2
  • Welcome to SO. Please check [how to ask a question](https://stackoverflow.com/help/how-to-ask). 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? – Al-un Mar 15 '19 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 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! – ANewUser Mar 16 '19 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. – ANewUser Mar 16 '19 at 16:22
  • 1
    Please post the code in your question, you can edit it ^^ – Al-un Mar 17 '19 at 09:07
  • As for your question itself, you may have a look at https://stackoverflow.com/questions/7165749/open-file-in-a-relative-location-in-python – Al-un Mar 17 '19 at 16:40

2 Answers2

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.

Joey
  • 1,436
  • 2
  • 19
  • 33
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

chaim glancz
  • 71
  • 1
  • 2