Iman
Iman

Reputation: 83

How to specify file path in jupyter notebook

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?

Upvotes: 3

Views: 105740

Answers (8)

aVral
aVral

Reputation: 133

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))

Upvotes: 0

Wais Yousofi
Wais Yousofi

Reputation: 19

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")

Upvotes: 0

ezChx
ezChx

Reputation: 4062

  1. 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
  2. How can I specify the file location?

    • csvFile = '{path_from_above}/myData.csv'

      For example: 'C:/Users/Iman/Documents/myData.csv'

Upvotes: 0

DonCarleone
DonCarleone

Reputation: 829

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.

Upvotes: 0

Anthony Vinay
Anthony Vinay

Reputation: 647

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');

Upvotes: 2

Sivasankar Boomarapu
Sivasankar Boomarapu

Reputation: 188

Specify the path as below

fp = open("/Users/siva/Desktop/siva5.txt")

Upvotes: 0

Iman
Iman

Reputation: 83

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'

Upvotes: 0

  1. If the author is directly calling the file then it is in the same folder where the Jupyter Notebook is running

  2. 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"

Upvotes: 2

Related Questions