Created
October 29, 2017 09:22
-
-
Save hoanbka/ca36d601913482a2d56b8b76dccad192 to your computer and use it in GitHub Desktop.
Read dir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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