How to Specify File Path in Jupyter Notebook

How to specify file path in jupyter notebook

I solved this problem by mounting the Google Colab to Google Drive.
This is the path after I mount to Google Drive:

csvFile = '/content/drive/My Drive/Colab Notebooks/myData.csv.txt'
xmlFile = '/content/drive/My Drive/Colab Notebooks/myData.xml'

Jupyter Notebook specify path to directory for concatenation of multiple .csv files

os.listdit(dir) lists the files in the path dir. In your example, you have dir='.', which corresponds to the current working directory (the directory from where you run your script). You can change this variable to the directory where your .csv files reside.

import pandas as pd
import os

base_dir = os.path.join('path', 'to', 'files')
filepaths = [f for f in os.listdir(base_dir) if f.endswith('.csv')]
df = pd.concat(map(pd.read_csv, filepaths)
Slower version with globbing

You can avoid using endswith() by globbing,

import pandas as pd
import os
import glob

base_dir = os.path.join('path', 'to', 'files')
filepaths = [f for f in glob.glob(f'{base_dir}*.csv')]
df = pd.concat(map(pd.read_csv, filepaths))

This expands the wildcard * to find all files that ends with .csv in base_dir.

How to set the running file path of jupyter in VScode?

I'm one of the developers on this extension. By default we follow the VSCode pattern of working directory as opposed to the Jupyter pattern. Meaning that we use the root of the currently open workspace folder as the current working directory for starting jupyter notebooks. That might be what is confusing you here.

To get around this you can either set cwd in your notebook code as redhatvicky mentioned or you can change the default current working directory in the following VSCode setting.

Jupyter -> Notebook File Root

Since you can change that setting per workspace you can have it always default to a specific location when working in just the workspace that contains your file.

Edit 2/16/22 New setting location

Related Topics

Leave a reply