3

I'm new to python and I'm trying to learn it using Google Colab I'm trying to open a CSV file, but I just can't do it.

import csv
open(r'C:\Users\User\Desktop\username.csv')

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-30-d948a291431c> in <module>()
      1 import csv
----> 2 open(r'C:\Users\User\Desktop\username.csv')

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\User\\Desktop\\username.csv'

I've already searched for similar questions, mostly of them sugest the problem is the (r'C:\completelocalfile), but I already tried, as you can see on my code, however it's not working for me. Any sugestions?

lipao255
  • 51
  • 1
  • 1
  • 4
  • 1
    So you're trying to open an existing file for reading, right? Have you verified that the file exists there with that exact name, and that you don't have extensions hidden in file explorer, which could hide the fact that its name is actually like `username.csv.txt` or something? If you open a command prompt to the desktop and run `dir`, is the file there with the expected name and extension? – Random Davis Mar 30 '21 at 21:18
  • Yes, it's the exactly same name, exactly same extension, I've downloaded four different csv files on google, if I try to open then they show the same error on python – lipao255 Mar 30 '21 at 21:23
  • 1
    Can you show the relevant part of the output of the `dir` command in your post so we can have all the info needed to help debug the issue? – Random Davis Mar 30 '21 at 21:27
  • C:\Users\User>cd desktop C:\Users\User\Desktop>dir O volume na unidade C não tem nome. O Número de Série do Volume é 0C71-96CE Pasta de C:\Users\User\Desktop 30/03/2021 18:02 176 username.csv 11/12/2020 10:26 14.094.055 weatherAUS.csv 18 arquivo(s) 17.223.116 bytes 9 pasta(s) 164.850.257.920 bytes disponíveis I've tried with excel files and the same error accurs – lipao255 Mar 30 '21 at 21:30

1 Answers1

2

Does this help? Basically, Google Colab cannot access your local machine files.

As a workaround, I would suggest to upload file on you drive, mount your drive in Colab, and then load the file from there.

The snippet to mount drive is the following:

from google.colab import drive
drive.mount('/content/drive', force_remount=True)

After that, you can upload the file manually and finally you load the file with the following command:

filename = r'/content/drive/MyDrive/pathtoyourfile'
import csv
open(filename)
SilentCloud
  • 1,677
  • 3
  • 9
  • 28
  • I've followed your instructions, and I still got the same error after all filename = r'/content/drive/MyDrive/ColabNotebooks/username.csv' import csv open(filename) --------------------------------------------------------------------------- FileNotFoundError Traceback (most recent call last) in () 1 filename = r'/content/drive/MyDrive/ColabNotebooks/username.csv' 2 import csv ----> 3 open(filename) FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/MyDrive/ColabNotebooks/username.csv' – lipao255 Mar 30 '21 at 21:54
  • I've never thought that opening a stupid file would take so much effort, it really makes us think about giving up, Jesus Christ – lipao255 Mar 30 '21 at 21:57
  • Try to put the `username.csv` file in you main drive folder and NOT in the ColabNotebooks folder, then change the `filename` line with the following code: `filename = r'/content/drive/MyDrive/username.csv'`. I tried it now and it works for me. – SilentCloud Mar 30 '21 at 22:03
  • If I use r'/content/drive/MyDrive/username.csv' then it show this error <_io.TextIOWrapper name='/content/drive/MyDrive/username.csv' mode='r' encoding='UTF-8'> However, my google drive is on portuguese, If i try r'/content/drive/MeuDrive/username.csv' (Meu Drive means My Drive) then it shows the same error before FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/MeuDrive/username.csv' – lipao255 Mar 30 '21 at 22:11
  • Ok I got it now: `/content/drive/MyDrive/ColabNotebooks/username.csv`, if "ColabNotebooks" is the default one provided in Drive, there's an error: you should put a blank space between "Colab" and "Notebooks". So this would be the full path: `/content/drive/MyDrive/Colab Notebooks/username.csv`. I tried it and it works – SilentCloud Mar 30 '21 at 22:11
  • `<_io.TextIOWrapper name='/content/drive/MyDrive/username.csv' mode='r' encoding='UTF-8'>` is not an error. Look at the answer in the following [post](https://stackoverflow.com/questions/38744244/glob-error-io-textiowrapper-name-mode-r-encoding-cp1252-reading-tex) – SilentCloud Mar 30 '21 at 22:14
  • I got this error, I've transfered the files back to the paste and ran <_io.TextIOWrapper name='/content/drive/MyDrive/Colab Notebooks/username.csv' mode='r' encoding='UTF-8'> – lipao255 Mar 30 '21 at 22:15
  • Finally made it, Thank you. God bless you. – lipao255 Mar 30 '21 at 22:20
  • Hi @FelipeBuongermino if this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – SilentCloud Apr 22 '21 at 12:35