Skip to content

Instantly share code, notes, and snippets.

@baskaufs
Created September 9, 2022 18:25
Show Gist options
  • Select an option

  • Save baskaufs/2c034f0cea843ad1f116d3d94270c4d3 to your computer and use it in GitHub Desktop.

Select an option

Save baskaufs/2c034f0cea843ad1f116d3d94270c4d3 to your computer and use it in GitHub Desktop.
Loop throrough directories and subdirectories to access every file
import os
base_path = '.'
directories = os.listdir(base_path + '/')
# directory outer loop
for directory in directories[:3]: # delete the index (square brackets and contents) to do all
if directory =='xml_jsonl_converter-master': # skip this directory, which doesn't have files to be processed
continue
if os.path.isdir(base_path + '/' + directory):
print(directory)
try:
subdirectories = os.listdir(base_path + '/' + directory)
# subdirectory middle loop
for subdirectory in subdirectories[:3]: # delete the index (square brackets and contents) to do all
if os.path.isdir(base_path + '/' + directory + '/' + subdirectory):
print('\t', subdirectory)
try:
files = os.listdir(base_path + '/' + directory + '/' + subdirectory)
# file inner loop
for file in files[:3]: # delete the index (square brackets and contents) to do all
try:
if os.path.isfile(base_path + '/' + directory + '/' + subdirectory + '/' + file):
# *************** Begin insert code
# Get the timestamp for the file
full_path = base_path + '/' + directory + '/' + subdirectory + '/' + file
print('\t\t', file)
# *************** End insert code
except:
pass # skip if access is denied
print()
except:
pass # skip directories where access is denied
print()
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment