Skip to content

Instantly share code, notes, and snippets.

@hoanbka
Created October 29, 2017 09:22
Show Gist options
  • Save hoanbka/ca36d601913482a2d56b8b76dccad192 to your computer and use it in GitHub Desktop.
Save hoanbka/ca36d601913482a2d56b8b76dccad192 to your computer and use it in GitHub Desktop.
Read dir
import os
def listAllFiles(path):
files = []
try:
for i in os.listdir(path):
files.append(i)
except FileNotFoundError:
print('File not found')
while len(files) > 0:
temp = files.pop()
if os.path.isdir(path + '\\''' + temp):
childs = os.listdir(path + '\\''' + temp)
for i in childs:
files.append(temp + '\\''' + i)
else:
print(path + '\\''' + temp)
path = 'D:\Python'
listAllFiles(path)
#####
# os.path.isdir('filePath') check the file is dir or not
# os.path.isfile('filePath') check the file is file or not
# os.listdir('filePath') list all files in a dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment