i have tried to import an excel csv file from my local disk but failed
import pandas as pd
df=pd.read_csv(r'C:/Users/Username/Desktop/da/SeoulBikeData.csv',encoding="gbk")
print("df")
error:
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/Username/Desktop/da/SeoulBikeData.csv'
First you need to load file into local directory of colab
here you can click on that folder
Click on icon of upload and upload the file you want to read
import pandas as pd
df=pd.read_csv(r'fummy.csv',encoding="gbk")
df
Finally your file will be loaded.
But there is limitation whenever you close your notebook on colab you have to done whole process again
better way to do this is upload file on drive and mount the drive and load file from drive:
from google.colab import drive
drive.mount('/content/drive')
After that:
df= pd.read_csv("copy path file here")
Third Way to load file in google colab is:
import io
from google.colab import files
uploaded = files.upload()
df2 = pd.read_csv(io.BytesIO(uploaded['fummy.csv']))
Click on choose file located directory of your file
Try uploading files on drive first.
Then read.
Go to file.
Copy path.
Try following code.
from google.colab import files
Upload= files.upload
Df= pd.read_csv("file_path_file_name")
df.head
You cannot import local directory excel files directly in Google collab. You have to upload the excel file in the Collab directory and then you can import it.
In Google Collab you have to upload it in sample_data folder. But the file will lost after you close it. Other alternatives are jupyter notebook where you can do everything in google collab but offline.
But if you want to use google collab here's where you need to upload
Files >> sample_data >> upload files
Try uploading files on drive first. Then read. Go to file. Copy path. Try following code according to mu method in then below
from google.colab import drive
drive.mount('/content/drive')
after that:
df= pd.read_csv("copy path file here")
Related
Someone gave me access to their folder that contains an .ipynb notebook and several folders with files. I downloaded it and uploaded it in my Drive. Two of the sub-folders that I need are Subfolder_A, with a few .txt files, and Subfolder_B, which has other subfolders containing some machine learning models. I later uploaded some extra text files in Subfolder_A, for example mytext.txt.
I gave the path:
path = "/content/gdrive/MyDrive/The_Folder/"
t5 ="t5_ml_file"
ml_path = path + "Subfolder_B/" + t5
textname= path + "Subfolder_A/" + "mytext.txt"
f = open(textname,"r")
full_text = f.read()
I get an error on the third to last line, saying it doesn't find the mytext.txt file. I think the path is given correctly because otherwise I would have had an error on the ml_path line. The exact error:
FileNotFoundError: [Errno 2] No such file or directory: '/content/gdrive/MyDrive/The_Folder/Subfolder_A/mytext.txt'
It doesn't work for any of my new uploaded text, only for the one that was already there, given by the person who gave me access to it.
Did you mount the drive ?
Before calling it you must mount the drive:
from google.colab import drive
drive.mount('/content/gdrive', force_remount=True)
In my case, it was solved by installing Ipython on my collab, so it will show up the new files added after the environment is already running.
!pip install Ipython --upgrade
So then, for new files, I run:
%load_ext autoreload
%autoreload 2
I am using Google Colab and my code has the following local path to my CSV file in my laptop:
path_csv = "C:\\Users\\Desktop\\data.csv"
I have linked my Google Drive to my notebook in Colab by using the following code:
from google.colab import drive
drive.mount('/content/gdrive')
And I have uploaded my CSV file to Google Drive.
My question is what to put instead of "C:\\users\\...." to make it work in Google Colab?
I have tried replacing C:\\Users\\Desktop\\data.csv with /gdrive/my drive/Users\\Desktop\\data.csv" but I get error message that the file is not found.
FileNotFoundError: [Errno 2] No such file or directory:
You can browse the contents of your mounted Google Drive folder using:
from google.colab import drive
drive.mount('/content/drive')
!ls /content/drive/
which for me outputs:
MyDrive
Going further:
!ls -l /content/drive/MyDrive/
which for me outputs:
total 41
drwx------ 2 root root 4096 May 4 2017 ...
-rw------- 1 root root 18 Apr 11 02:37 data.csv
...
Note that you should be using / instead of a \ everywhere in your paths, as Google Colab is a Linux-based system (see Why does Windows use backslashes for paths and Unix forward slashes?). Also, the / at the start of the path is important. Basically, the same mount path you passed to drive.mount should be exactly the same base path you use everywhere else.
So, if you uploaded your data.csv at the top-level/root of your Google Drive folder ('/content/drive/MyDrive'), then it should also show up there at the top-level/root of the /content/drive/MyDrive directory.
path_to_csv = '/content/drive/MyDrive/data.csv'
with open(path_to_csv) as f:
for line in f.read().splitlines():
print(line)
I have tried replacing C:\Users\Desktop\data.csv with /gdrive/my drive/Users\Desktop\data.csv" but i get error message that is not found.
Google Drive does not follow the same folder structure as what you have on your local PC. The best way to visually see which files and folders are available and how they are organized is to open your Drive on your browser at: https://drive.google.com/drive/my-drive
So, for example, if you placed data.csv at My Drive > TEST > DATA:
Then the corresponding path would be:
# Check the root folder
!ls /content/drive/
# Path should be visually same as in drive.google.com
!ls /content/drive/MyDrive/TEST/DATA
path_to_csv = '/content/drive/MyDrive/TEST/DATA/data.csv'
MyDrive
data.csv
For more information on working with Google Drive in Colab, see the tutorial/docs on External data: Local Files, Drive, Sheets, and Cloud Storage:
The example below shows how to mount your Google Drive on your runtime using an authorization code, and how to write and read files there. Once executed, you will be able to see the new file (foo.txt) at https://drive.google.com/.
So just as #Gino Mempin said, it is running on a cloud system and it uses a different path, which is totally different compared to Windows paths on your local machine.
Mount the Google Drive and open the left panel and go to your file location and click on it. There you will have a Copy Path option:
In addition, If you want to run your code inside a specific directory you can make use of this command:
%cd path_to_directory
FYI for Window users, Linux systems such as Google Colab use the "/" seperator instead of "\", so content\drive\MyDrive\data.csv won't work and need to become content/drive/MyDrive/data.csv.
"My Drive" <-- See SPACE in name.
All other answers posting wrong "MyDrive" which not work.
/content/drive/My Drive/sd-v1-4.ckpt
I load dataset with tar.gz file from website in pyspark.
dataset=spark.sparkContext.textFile('https://www.example/example.tar.gz') (the url is just an example)
and
dataset.collect()
get error.
You cannot load files directly into core spark from website. You have to download files from website to your local file system and load it as follows
dataset=spark.sparkContext.textFile("file:///your file local file path")
or after placing files in hdfs using
dataset=spark.sparkContext.textFile(" your hdfs file path")
I want to mount google drive on google Colab and I am using this command to mount the drive
from google.colab import drive
drive.mount('/content/drive/')
but I am getting this error
ValueError Traceback (most recent call last)
<ipython-input-45-9667a744255b> in <module>()
1 from google.colab import drive
----> 2 drive.mount('content/drive/')
/usr/local/lib/python3.6/dist-packages/google/colab/drive.py in
mount(mountpoint, force_remount)
99 raise ValueError('Mountpoint must either be a directory or not exist')
100 if '/' in mountpoint and not _os.path.exists(_os.path.dirname(mountpoint)):
--> 101 raise ValueError('Mountpoint must be in a directory that exists')
102 except:
103 d.terminate(force=True)
ValueError: Mountpoint must be in a directory that exists
#clarky: the error you got was correct tried to tell you that your usage of drive.mount() is incorrect: the mountpoint argument to drive.mount() must be an empty directory that exists, or the name of a non-existent file/directory in a directory that does exist so that the mountpoint can be created as part of the mount operation. Your usage of a relative path in drive.mount('content/drive/') (i.e. content/drive/) implies that the mount should happen at '/content/content/drive' because the interpreter's default path is /content; note the doubled content path component there, and likely you don't already have a directory named /content/content inside of which a mountpoint named drive could be created. The fix to your notebook code is to instead use drive.mount('/content/drive') - note the leading / making the mountpount path absolute instead of relative.
Just go to "manage section" , then terminate your current section, and try to mount again with:
from google.colab import drive
drive.mount('/content/drive', force_remount=True)
It worked here.
I ran into this error this morning as well. I'm not sure what this commit what meant to fix but it certainly caused the error. A workaround is to copy the code for drive.py into colab, comment out lines 100 and 101 like this:
# drive.py
...
try:
if _os.path.islink(mountpoint):
raise ValueError('Mountpoint must not be a symlink')
if _os.path.isdir(mountpoint) and _os.listdir(mountpoint):
raise ValueError('Mountpoint must not already contain files')
if not _os.path.isdir(mountpoint) and _os.path.exists(mountpoint):
raise ValueError('Mountpoint must either be a directory or not exist')
# if '/' in mountpoint and not _os.path.exists(_os.path.dirname(mountpoint)):
# raise ValueError('Mountpoint must be in a directory that exists')
except:
d.terminate(force=True)
raise
...
then replace
from google.colab import drive
drive.mount('content/drive/')
with
mount('/content/drive/')
using the mount function you copied from drive.py
Hopefully the issue gets fixed quickly enough so we can do away with this workaround.
Simply use:
from google.colab import drive
drive.mount("/content/gdrive")
instead of:
from google.colab import drive
drive.mount("/content/drive/")
In my case, I click the folder icon on the side panel, it will show you Upload, Refresh and Mount Drive.
click on the Mount Drive, the 'drive' folder containing 'My Drive'
folder appears
Then run
from google.colab import drive
drive.mount('drive')
Go to this URL in a browser will appear - I sign to one of my account
Enter your authorization code
Drive
I received the error as well change to drive.mount('/content/drive')
Replace drive.mount('/content/drive/') by drive.mount('/content/drive')
just remove the '/' following the drive and it works perfectly..
That is from drive.mount('/content/drive/') to drive.mount('/content/drive')
WARNING: Make sure to read my explanation before running the command below
I ran into this error today and the reason was that Google Colab for some reason kept some folders and files from the previous session (probably because I created the folders from within the notebook). These files and folders were still being shown via the "Folders" menu, but no other GDrive files were, as I hadn't authenticated again. Even the "force_remount=True" option didn't work.
To fix this, I simply deleted the remaining files from /drive/ by running the following command:
! rm -rf drive/
Then I could mount my GDrive again on the /drive/ directory:
from google.colab import drive
drive.mount('/content/drive')
Run command to unmount drive first.
!fusermount -u drive
Then try run again,
from google.colab import drive
drive.mount('/content/drive')
If mounting does not work even if absolute path /content/drive was used, then verify that appropriate directories exist,
!mdkir -p /content/drive
This can happen if you haven't mounted the drive previously but had a path that led to saving your data in the drive. So now, as colab doesn't have access to your drive, it will create a directory with the same names as your path and then save it in the colab session. Now, if you want to mount the drive now, it will have issues because the same path is referred but to two different locations.
Easy fix for this is to:
a.) Delete the files from your sessions in the colab
or
b.) Rename drive named folder in your colab session.
Now try to mount again. You should be good to go.
%%javascript
IPython.OutputArea.prototype._should_scroll = function(lines) {
return false;
}
%run rl_base.py
I run this giving error saying rl_base.py file not found. I have uploaded the same to gdrive in colab and from the same folder I am running my .ipynb file, containing the above code
If you have the test.py file in the corresponding folder in drive as in the below attached image, then the command which you use to run the test.py file is as mentioned below,
!python gdrive/My\ Drive/Colab\ Notebooks/object_detection_demo-master/test.py
Additional Info:
If you jusst want to run !python test.py then you should change directory, by the following command before it,
%cd gdrive/My\ Drive/Colab\ Notebooks/object_detection_demo-master/
When you run your notebook from Google drive, an instance is created only for the notebook. To make the other files in your Google drive folder available you can mount your Google drive with:
from google.colab import drive
drive.mount('/content/gdrive')
Then copy the file you need into the instance with:
!cp gdrive/My\ Drive/path/to/my/file.py .
And run your script:
!python file.py
You should not upload to gdrive. You should upload it to Colab instead, by calling
from google.colab import files
files.upload()
## 1. Check in which directory you are using the command
!ls
## 2.Navigate to the directory where your python script(file.py) is located using the command
%cd path/to/the/python/file
## 3.Run the python script by using the command
!python file.py
A way is also using colabcode.. You will have full ssh access with Visual Studio Code editor.
# install colabcode
!pip install colabcode
# import colabcode
from colabcode import ColabCode
# run colabcode with by deafult options.
ColabCode()
# ColabCode has the following arguments:
# - port: the port you want to run code-server on, default 10000
# - password: password to protect your code server from being accessed by someone else. Note that there is no password by default!
# - mount_drive: True or False to mount your Google Drive
!ColabCode(port=10000, password="abhishek", mount_drive=True)
It will prompt you with a link to visual studio code editor with full access to your colab directories.
Here is a simple answer along with a screenshot
Mount the google drive
from google.colab import drive
drive.mount('/content/drive')
Call the py file path
import sys
import os
py_file_location = "/content/drive/MyDrive/Colab Notebooks"
sys.path.append(os.path.abspath(py_file_location))
It seems necessary to put the .py file's name in ""
!python "file.py"