Visual Studio Code - Opening Files with Python open()

See Python: Tips and Tricks for similar articles.

When running Python files using Run Python File in Terminal, Visual Studio Code runs the Pythn file using an absolute path. For example:

python "/Users/natdunn/Documents/Webucator/ClassFiles/file-processing/Solutions/list_creator.py"

Follow these instructions to set up Visual Studio Code to run the file from the directory it is in.

  1. Select Settings from the Code > Preferences menu: Code > Preferences > Settings
  2. Add the following line to your User Settings:
    "python.terminal.executeInFileDir": true,
    User Settings
  3. The next time you run a Python file using Run Python File in Terminal, it will change the directory before executing the file:
    Nats-MacBook-Pro:Webucator natdunn$ cd "/Users/natdunn/Documents/Webucator/ClassFiles/file-processing/Solutions"
    Nats-MacBook-Pro:Solutions natdunn$ python "/Users/natdunn/Documents/Webucator/ClassFiles/file-processing/Solutions/list_creator.py"

Happy Pythoning!