Google Colab - how should we move the current directory to the directory of the present file?

Hi,

In Google Colab, when we type “!pwd”, we always get “/content”. It takes pains and is unnatural for me to move the current directory to “the directory where the present file is located” as follows:

from google.colab import drive
drive.mount(’/gdrive’)
%cd /gdrive/MyDrive/XYZ

What is worse, if one reorganize the present folder, we will have to do the above again. Is there easy way to  move the current directory to the directory of the present file, so that we can read the csv, xlsx file from the folder?

 I am not sure whether the two cases are different: (1) file under Colab Notebooks folder (2) file elsewhere in MyDrive. 

I saw a few discussions on "Google Colab" in the forum, so I asked it here. If there are better place to post, please kindly let me know and I would abide by the practice here.

By John M via Discussions on Python.org at 26Apr2022 06:08:

In Google Colab, when we type “!pwd”, we always get “/content”. It
takes pains and is unnatural for me to move the current directory to
“the directory where the present file is located” as follows:

from google.colab import drive
drive.mount(‘/gdrive’)
%cd /gdrive/MyDrive/XYZ

That looks a little odd. 2 Python statements, then a shell command?

What is worse, if one reorganize the present folder, we will have to do the above again. Is there easy way to move the current directory to the directory of the present file, so that we can read the csv, xlsx file from the folder?

Well, you can just do a chdir in Python:

os.chdir('/gdrive/MyDrive/XYZ')

You’d probably parameterise that:

from google.colab import drive

WORK_DRIVE = '/gdrive'
WORK_AREA = WORK_DRIVE + '/MyDrive/XYZ'

drive.mount(WORK_DRIVE)
os.chdir(WORK_AREA)

If that is some common setup script run by Colab in some sense, that
might be ok.

However, normally we try to avoid using os.chdir because it is a
process global thing. If you use relative paths for anything, they
inherently depend on your working directory. If one of your functions
does a chdir in order to use a relative filename, then other places
might now use the wrong files (or fail to find their files) if they also
use relative filenames.

Instead we usually just construct the full paths to what we need to
access. Example:

import os.path

def process_file(filename, workarea=None):
    if workarea is not None:
        filename = os.path.join(workarea, filename)
    ... work on the file in filename now, which has a full path ...

... at the calling end ...
process_file('foo.csv', workarea==WORK_AREA)

Ths avoiding using chdir at all.

I am not sure whether the two cases are different: (1) file under 
Colab Notebooks folder (2) file elsewhere in MyDrive.

I’m not a Colab user.

I saw a few discussions on “Google Colab” in the forum, so I asked
it here. If there are better place to post, please kindly let me
know and I would abide by the practice here.

“users” is a fine place unless there is a special purpose other forum
for Colab, which there may not be. So here is fine.

Cheers,
Cameron Simpson cs@cskk.id.au

Thank you very much for your reply. Is there anyway that I can avoid typing “XYZ” explicitly?

In an offline, regular jupyter notebook, I can read csv or xlsx file in the same folder. If I move the whole folder (containing ipynb, xlsx, csv) elsewhere, I can still work similarly. If I need to type “XYZ”, I need to do it whenever I move or rename the folder.