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 globbingYou 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
Convert Numbers into Corresponding Letter Using Python
No Output Displays When Execute Python File
Remove Partial String from Dataframe With Pandas
Checking If Particular Value (In Cell) Is Nan in Pandas Dataframe Not Working Using Ix or Iloc
Easiest Way to Convert Two Columns to Python Dictionary
Most Pythonic Way to Kill a Thread After Some Period of Time
How to Get All Possible Combinations of a List'S Elements
How to End Program If Input == "Quit" With Many If Statements
How to Print Only the Last Value in a for Loop
How to Iterate Through a List of Dictionaries in Jinja Template
How to Make a Function Change Variables While in a While Loop
How to Restart a Program Based on User Input
How to Install Pip for a Specific Python Version
Python - Automatically Adjust Width of an Excel File'S Columns
Delete Every Non Utf-8 Symbols from String
How to Find 3 Immediate Words After Keyword Match Using Python