Many times we need to create a searching utility program which when provided a directory path lists all the files and sub-directories inside it.
Python provides many utility functions to list folders and files in a directory.
Method 1: Using os.listdir
os module has a function listdir
which takes the path of directory as argument and returns a list of all files and directories inside it.
This function returns a list of names of files and folders. If you want a complete path of both files and folders, then use path function of os.path
module to create it.
Example,
import os
file_list = os.listdir('f:/python')
print('--- Names of files ---')
for file in file_list:
# print file and folder names
print(file)
print('--- Absolute path of files ---')
for file in file_list:
# print complete path
print(os.path.join('f:/python',file))
Above code prints following output
— Names of files —
code.py
code.py___jb_old___
code.zip
data.json
db.py
example.txt
modules
output.PNG
tutorial
— Absolute path of files —
f:/python/code.py
f:/python/code.py___jb_old___
f:/python/code.zip
f:/python/data.json
f:/python/db.py
f:/python/example.txt
f:/python/modules
f:/python/output.PNG
f:/python/tutorial
If you want to filter the list to contain only files or folders, then use the isfile
and isdir
functions of os.path
module respectively. Below example is a modification of above code to contain only files.
import os
file_list = os.listdir('f:/python')
print('--- Names of files ---')
for file in file_list:
# print only files
if(os.path.isfile(os.path.join('f:/python',file))):
print(file)
Remember that isfile
function will work only when absolute path of the file or folder is provided to it.
Method 2: Using glob module
Python’s glob module has a glob
function which takes a path as argument. This path should contain a pattern at the end.
Pattern would consist of wild cards and it will return only files that match the pattern. Such as path ending with *.txt will return only files with .txt extension.
In order to match all files and folders in a directory, the pattern would end with a *. Example,
import glob
# match all contents of folder
file_list = glob.glob('f:/python'+ '/*')
# iterate over the list
for f in file_list:
print(f)
Above code will output
f:/python/code.py
f:/python/code.py___jb_old___
f:/python/code.zip
f:/python/data.json
f:/python/db.py
f:/python/example.txt
f:/python/modules
f:/python/output.PNG
f:/python/tutorial
Return type of glob
function is a list. If you do not iterate and simply print its returned value, then you will get
‘f:/python\\code.py’, ‘f:/python\\code.py___jb_old___’, ‘f:/python\\code.zip’, ‘f:/python\\data.json’, ‘f:/python\\db.py’, ‘f:/python\\example.txt’, ‘f:/python\\modules’, ‘f:/python\\output.PNG’, ‘f:/python\\tutorial’]
Also note that this method returns the absolute path of files and folders by default as compared to os.listdir function.
Method 3: Using os.walk function
walk
function of os module takes the path of a directory as argument and returns a tuple of 3 elements in the below order
directory path: Path to the directory that was provided as an argument to walk.
directory names: Names of all sub-directories inside the provided path.
file names: Names of all files inside the provided directory.
Remember that walk
function traverses recursively. That is, it will list down all the files and directories which are inside other sub directories too.
import os
file_list = os.walk('f:/python')
# iterate over tuple
for root, folders, files in file_list:
# iterate over only files
for file in files:
print(file)
Above program will output
app1.py
code.py
code.py___jb_old___
code.zip
data.json
db.py
example.txt
output1.PNG
output2.PNG
Also, all the values in the tuple are only names. If you want an absolute path, then use os.path.join
as illustrated above.
Example,
import os
file_list = os.walk('f:/python')
# iterate over tuple
for root, folders, files in file_list:
# iterate over only files
for file in files:
print(os.path.join(root,file))
Output of above program is
f:/python/app1.py
f:/python/code.py
f:/python/code.py___jb_old___
f:/python/code.zip
f:/python/data.json
f:/python/db.py
f:/python/example.txt
f:/python/output1.PNG
f:/python/output2.PNG
Note that for displaying the complete path of files, you can also use print(root + '/' + path)
in place of print(os.path.join(root, file))
.
As stated above, walk
traverses recursively and will list all the files in all the folders, no matter how deep they are nested inside other folders.
If you want to list files up to only one level, then terminate the loop after its first iteration using break
statement as shown below.
import os
file_list = os.walk('f:/python')
print('/n--- File list ---')
for root, folders, files in file_list:
for file in files:
print(file)
# terminate the loop
break
If you think this post helped you out, then do click the clap icon.