manju h
manju h

Reputation: 141

A list of downloaded files names in google colabarotary

enter image description here

How can i get all file names of downloaded files in google colaboratory

Upvotes: 0

Views: 1134

Answers (1)

ninjin
ninjin

Reputation: 1198

The default working directory of a Colab notebook is /content, so you can use os.listdir to get a list of the names of the entries in /content:

import os
DIR = "/content"

# if you want to list all the contents in DIR
entries = [entry for entry in os.listdir(DIR)]

# if you want to list all the files in DIR
entries = [entry for entry in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, entry))]

Then you can use a loop to print them:

for entry in entries:
    print(entry)

If you don't need to store this list in a variable, you can also use a bash command (note the ! at the start of the line):

# list all the contents in /content
!ls /content

# list all the files in /content (https://askubuntu.com/questions/811210/how-can-i-make-ls-only-display-files)
!ls -p | grep -v /

Upvotes: 2

Related Questions