Determining the path in Google Colab - python

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

Related

Unable to set working directory for Jupyter Lab on Mac

I'm working with Jupyter Lab on a Mac. I've been trying to change the working directory in a .ipynb script via os.chdir(), so I can use the code to iteratively process multiple files in the working directory.
Eventually, I would like to use os.listdir() to iterate through the files in the directory.
My laptop files show up properly in the menu on the left, but for some reason, the following code won't work:
# set working directory
os.chdir('C:/Users/glonati/Documents/2019-_UNB SJ/drone/programs_code/python')
path = os.getcwd()
It produces the error: FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/glonati/Documents/2019-_UNB SJ/drone/programs_code/python'
I've quadruple checked the path names and tried all combinations of '', "", as well as , \, and /.
I read that I should try modifying the jupyter config file. But when I download the file (via jupyter notebook --generate-config), the only lines in the file are:
c.NotebookApp.allow_credentials = False
c.NotebookApp.port = 8889
c.NotebookApp.password_required = True
There is no line for c.NotebookApp.notebook_dir = ''.
Unsure why I'm having so much trouble with this. Any insight would be greatly appreciated! Thanks ahead of time!

How to set the path to Google Drive files from Google Colab?

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

Python unable to move directories using shutil

I'm trying to write a script where it will move a directory (containing images to be processed) into another dedicated working directory (within the project folder) by using shutil.move().
However, my Python script keeps on failing to achieve this by throwing an [Errno 2] No such file or directory: '/Users/user/Desktop/Captured\\ Images' exception.
The target directory is being perfectly recognised so that's not the issue.
I'm currently using macOS to develop this script, and using standard BASH.
This is how I input the path of the source directory (by dragging the folder into the terminal): /Users/user/Desktop/Captured\ Images.
This is how Python interprets the source directory: /Users/user/Desktop/Captured\\ Images.
The script works perfectly when I modify the source path to: /Users/user/Desktop/Captured Images.
I've also tried using the Pathlib Module to prevent file separator issues, but that also didn't work.
I know for sure that what's causing the problem is the \ within the file path since the folder is named 'Captured Images' with a space between it, thus Captured\ Images.
Here's the following source code:
move_directory(input("Please Input The Directory Of The Captured Images You Would Like To Import: "))
def move_directory(source_directory):
try:
shutil.move(source_directory, os.path.dirname(os.path.dirname(__file__)) + '/Captured Images/Source Images')
except OSError as errorMessage:
print("Failed To Move Directory: {0}".format(errorMessage))
Update 1:
I've just tried to move the directories by using subprocess.run(["mv", ...]) and it worked, so now I see that it's not how the script is taking in the input, but a problem between the path input and shutil.move().

Google Colab-ValueError: Mountpoint must be in a directory that exists

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.

Why do I get a "no such file or directory" error when the file is known to exist?

I have uploaded a package to pypi and github. I have then installed the package and tried to use it. It contains a python script which need to read from a file. I have placed both in the same directory.
pip install pycricket
from pycricket import cricket
c = cricket.Cricket()
c.query()
Query() function involves reading from a file. When I see the 'pycricket' package in library, both script as well as file are in same folder.
query():
with open('matches.csv', 'r') as f:
#code
I don't know why I get the error.
You can inspect the current working directory with:
>>> import os
>>> os.getcwd()
If your data is in a different directory (unclear from the question, but likely given the error message), then change to the directory where the data is stores:
>>> os.chdir(path_to_data_directory)

Categories