Visual Studio Code: FileNotFoundError. Execute in File Dir broken, and unable to access file's absolute path

Question:

I am trying to open a CSV file without using its absolute path, but I cannot access it because my files are located in /Users/foo/Machine Learning/Multiple Linear Regression/, which is a child of my working directory /Users/foo/Machine Learning/Machine Learning.
I open a Workspace with the parent directory, and when I run my python code:

#1. Importing the Libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

#2. Data Acquisition"""

File = '50_AdAgency.csv'
dataset = pd.read_csv(File)

I get this error:

FileNotFoundError: [Errno 2] No such file or directory: '50_AdAgency.csv'

I do not get this issue when I open my Workspace as the directory the files are located in, but I would like to be able to open my Workspace from the parent directory and run my files such that they execute in the file’s directory, instead of the current Workspace folder.

There is a setting for this that I tried to change: Python › Terminal: Execute In File Dir
After selecting it, I am still getting the same error.
Double checking the settings.json file, this setting is in effect:

{
    "python.defaultInterpreterPath": "/usr/local/bin/python3",
    "editor.minimap.enabled": false,
    "security.workspace.trust.untrustedFiles": "open",
    "[python]": {
        "editor.formatOnType": true
    },
    "notebook.lineNumbers": "on",
    "python.terminal.executeInFileDir": true
}

I gave up on trying to get VS Code to open the file from its own directory and instead tried to refer to its absolute path by doing this:

import os
filename = '50_AdAgency.csv'
realpath = os.path.realpath(__file__)
fileDir = os.path.dirname(realpath)
dataset = pd.read_csv(os.path.join(fileDir, filename))

However, I get this error: NameError: name '__file__' is not defined. Did you mean: '__name__'?

So I switched it to __name__ and added print statements just to get more insight.

import os
filename = '50_AdAgency.csv'
realpath = os.path.realpath(__name__)
print(realpath)
fileDir = os.path.dirname(realpath)
print(fileDir)
#dataset = pd.read_csv(os.path.join(fileDir, filename))

and got this result:

>>> import numpy as np
>>> import pandas as pd
>>> import matplotlib.pyplot as plt
>>> import os
>>> filename = '50_AdAgency.csv'
>>> realpath = os.path.realpath(__name__)
>>> print(realpath)
/Users/vasyl/Desktop/School/Machine Learning/__main__
>>> fileDir = os.path.dirname(realpath)
>>> print(fileDir)
/Users/vasyl/Desktop/School/Machine Learning
>>> 

What I wanted to get was: /Users/vasyl/Desktop/School/Machine Learning/Multiple Linear Regression, but the the file’s directory isn’t recognized, it stops at its parent directory.

Not sure how to proceed next. Is there anyway to access the file’s actual directory?

Asked By: Vasyl Shevtsov

||

Answers:

You’ll get FileNotFound because VsCode runs files using the workspace as the root directory by default.

Obviously, what you want is to be able to make the file run in the same directory where this file stays.

You can open Settings(or shortcuts "ctrl+,") .

enter image description here

And search for python.terminal.executeInFileDir, then check it.

enter image description here

Answered By: MingJie-MSFT