Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Colab: How to loop through images in a folder?

Tags:

python

I've copied a large number of .jpg images from my Google Drive to Google Colab per Google Colab: how to read data from my google drive?:

local_download_path = os.path.expanduser('~/data')
try:
  os.makedirs(local_download_path)
except: pass

# 2. Auto-iterate using the query syntax
#    https://developers.google.com/drive/v2/web/search-parameters
file_list = drive.ListFile(
    {'q': "'1SooKSw8M4ACbznKjnNrYvJ5wxuqJ-YCk' in parents"}).GetList()

for f in file_list:
  # 3. Create & download by id.
  print('title: %s, id: %s' % (f['title'], f['id']))
  fname = os.path.join(local_download_path, f['title'])
  print('downloading to {}'.format(fname))
  f_ = drive.CreateFile({'id': f['id']})
  f_.GetContentFile(fname)

This completed, so all the image files should be in a data folder. How do I actually access these files now? I'd like to loop through all files that are of type .jpg in the data folder.

like image 744
ZhouW Avatar asked Aug 15 '18 11:08

ZhouW


1 Answers

One alternative would be to use the already implemented os library.

# Just needed in case you'd like to append it to an array
data = []

for filename in os.listdir(local_download_path):
    if filename.endswith("jpg"): 
        # Your code comes here such as 
        print(filename)
        data.append(filename)
like image 85
RandomDude90 Avatar answered Sep 22 '22 11:09

RandomDude90