How to specify file path in jupyter notebook

32,419

Solution 1

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'

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

Share:
32,419
Iman
Author by

Iman

Updated on July 18, 2022

Comments

  • Iman
    Iman almost 2 years

    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?