How to specify file path in jupyter notebook
Question:
I have problem in specifying path of my file in jupyter notebook/google colab. This is the example code I found:
import csv
csvFile = 'myData.csv'
xmlFile = 'myData.xml'
csvData = csv.reader(open(csvFile))
xmlData = open(xmlFile, 'w')
I do not know where the author of the code above place the myData.csv, so I have tried this code to locate my file:
csvFile = 'C:Users...myData.csv'
but I get this error:
SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated UXXXXXXXX escape
I also have tried this code:
csvFile = r'C:Users...myData.csv'
but I get this error:
FileNotFoundError: [Errno 2] No such file or directory: ‘C:Users…myData.csv’
My questions are:
1. Where the author of the code above place the myData.csv?
2. How can I specify the file location?
Answers:
-
If the author is directly calling the file then it is in the same folder where the Jupyter Notebook is running
-
One of the following should work to call files from different locations:
a. Replace single quotes with double quotes and escape the slashes
ex. csvFile = "C:\Users\...\myData.csv"
b. Replace single quotes with double quotes and use forward slashes
ex. csvFile = "C:/Users/.../myData.csv"
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'
Specify the path as below
fp = open("/Users/siva/Desktop/siva5.txt")
I have tried using both Forward slash and also double backward slash. Both works.
'C:\Users\SAVK\Downloads\Ex_Files_Intro_Data_Science\Ex_Files_Intro_Data_Science\Exercise Files\state_baby_names.csv'
'C:/Users/SAVK/Downloads/Ex_Files_Intro_Data_Science/Ex_Files_Intro_Data_Science/Exercise Files/us_baby_names.csv'
Example :
states_babies = pd.read_csv('C:\Users\SAVK\Downloads\Ex_Files_Intro_Data_Science\Ex_Files_Intro_Data_Science\Exercise Files\state_baby_names.csv');
states_babies = pd.read_csv('C:/Users/SAVK/Downloads/Ex_Files_Intro_Data_Science/Ex_Files_Intro_Data_Science/Exercise Files/us_baby_names.csv');
If it is an issue with the filepath syntax try this:
import csv
csvfile = open('C:\Users\....\<your_filename.file_extenstion>', "r")
readCSV = csv.reader(csvfile)
Based on years of experience with
PEBCAK errors, something tells me that this file is not located where you think it is.
-
Where did the author of the code place myData.csv?
- Open File Explorer
- Click on the appropriate drive C:, D:, etc.
- Enter myData.csv in the search box
- Search and retrieve the file location
-
How can I specify the file location?
-
csvFile = ‘{path_from_above}/myData.csv’
For example: ‘C:/Users/Iman/Documents/myData.csv’
If you want to go one level up( parent directory) and after that going to another sub-directory then (..parent/child).
For eg:
it is a sub-directory inside the same parent but from one sub directory i am navigating to another file called datasets
movies = pd.read_csv("..datasets/movies.csv")
ratings = pd.read_csv("..datasets/ratings.csv")
I hope this could help!
# relative path to subfolder
path = "../power corr. factors/power corr"
for f in os.listdir(path):
data = pd.read_csv(os.path.join(path,f))
I have problem in specifying path of my file in jupyter notebook/google colab. This is the example code I found:
import csv
csvFile = 'myData.csv'
xmlFile = 'myData.xml'
csvData = csv.reader(open(csvFile))
xmlData = open(xmlFile, 'w')
I do not know where the author of the code above place the myData.csv, so I have tried this code to locate my file:
csvFile = 'C:Users...myData.csv'
but I get this error:
SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated UXXXXXXXX escape
I also have tried this code:
csvFile = r'C:Users...myData.csv'
but I get this error:
FileNotFoundError: [Errno 2] No such file or directory: ‘C:Users…myData.csv’
My questions are:
1. Where the author of the code above place the myData.csv?
2. How can I specify the file location?
-
If the author is directly calling the file then it is in the same folder where the Jupyter Notebook is running
-
One of the following should work to call files from different locations:
a. Replace single quotes with double quotes and escape the slashes
ex. csvFile = "C:\Users\...\myData.csv"
b. Replace single quotes with double quotes and use forward slashes
ex. csvFile = "C:/Users/.../myData.csv"
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'
Specify the path as below
fp = open("/Users/siva/Desktop/siva5.txt")
I have tried using both Forward slash and also double backward slash. Both works.
'C:\Users\SAVK\Downloads\Ex_Files_Intro_Data_Science\Ex_Files_Intro_Data_Science\Exercise Files\state_baby_names.csv'
'C:/Users/SAVK/Downloads/Ex_Files_Intro_Data_Science/Ex_Files_Intro_Data_Science/Exercise Files/us_baby_names.csv'
Example :
states_babies = pd.read_csv('C:\Users\SAVK\Downloads\Ex_Files_Intro_Data_Science\Ex_Files_Intro_Data_Science\Exercise Files\state_baby_names.csv');
states_babies = pd.read_csv('C:/Users/SAVK/Downloads/Ex_Files_Intro_Data_Science/Ex_Files_Intro_Data_Science/Exercise Files/us_baby_names.csv');
If it is an issue with the filepath syntax try this:
import csv
csvfile = open('C:\Users\....\<your_filename.file_extenstion>', "r")
readCSV = csv.reader(csvfile)
Based on years of experience with
PEBCAK errors, something tells me that this file is not located where you think it is.
-
Where did the author of the code place myData.csv?
- Open File Explorer
- Click on the appropriate drive C:, D:, etc.
- Enter myData.csv in the search box
- Search and retrieve the file location
-
How can I specify the file location?
-
csvFile = ‘{path_from_above}/myData.csv’
For example: ‘C:/Users/Iman/Documents/myData.csv’
-
If you want to go one level up( parent directory) and after that going to another sub-directory then (..parent/child).
For eg:
it is a sub-directory inside the same parent but from one sub directory i am navigating to another file called datasets
movies = pd.read_csv("..datasets/movies.csv")
ratings = pd.read_csv("..datasets/ratings.csv")
I hope this could help!
# relative path to subfolder
path = "../power corr. factors/power corr"
for f in os.listdir(path):
data = pd.read_csv(os.path.join(path,f))