3

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?

8 Answers 8

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Prachiti, unfortunately no luck
2

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

Comments

0

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'

Comments

0

Specify the path as below

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

Comments

0

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.

Comments

0
  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'

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.