Listing out directories and files in Python?

PythonServer Side ProgrammingProgramming

There are several ways to list the directories and files in python.

Listing Files in a directory

One of the easiest ways to get all the files or directories from a particular path is by using os.listdir() method.

 Live Demo

import os
for x in os.listdir('.'):
   print(x)

Result

.pytest_cache
4forces.json
annotation1.py
asyncWrite.txt
attribute_access.py
background_process.py
background_process2.py
BeautifulSoup_script1.py
bottle_exampl1.py
bottole_test1.py
build
built-in_funct.py
callable_objects1.py
cars.csv
classes_instance.py
class_attributes.py
class_attributes1.py
code_gmplot.py
config.py
data1.json
datafile.txt
……

Above code is showing a list of files and directories from a current working directory. If you want to list-files and directories from a particular directory, just pass the absolute pathname,

import os
for x in os.listdir(r'C:\Python\Python361\selenium'):
   print(x)

Result

geckodriver.log
test1.py
webdriver

The result is similar to our folder hierarchy −

Above output is not giving any indication of whether our output is file or directory or link etc. If you want to find whether the entry is a file, directory etc., we can use os.path.isfile() as shown below:

import os

for x in os.listdir('.'):
   if os.path.isfile(x): print ('file-', x)
   elif os.path.isdir(x): print ('directory-', x)
   elif os.path.islink(x): print ('link-', x)
   else: print ('---', x)

Output

directory- .pytest_cache
file- 4forces.json
file- annotation1.py
file- asyncWrite.txt
file- attribute_access.py
file- background_process.py
file- background_process2.py
file- BeautifulSoup_script1.py
file- bottle_exampl1.py
file- bottole_test1.py
directory- build
file- built-in_funct.py
file- callable_objects1.py
file- cars.csv
file- classes_instance.py
file- class_attributes.py
file- class_attributes1.py
file- code_gmplot.py
file- config.py
file- data1.json
file- datafile.txt
file- datawork
file- data_pandas1.csv
file- data_pandas1.xlsx
file- debugger_pdb.py
file- debugging_timeit1.py
file- debugging_timeit2.py
file- define_class.py
file- directoryTreeStruc.py
directory- dist
directory- django
directory- DLLs
directory- Doc
file- dynamic_array_implementation.py

We can have one-liner using filter() to collect the files from a particular path −

list(filter(lambda x: os.path.isfile(x), os.listdir('.')))

Output

['4forces.json', 'annotation1.py', 'asyncWrite.txt', 'attribute_access.py', 'background_process.py', 'background_process2.py', 'BeautifulSoup_script1.py', 'bottle_exampl1.py', 'bottole_test1.py', 'built-in_funct.py', 'callable_objects1.py', 'cars.csv', 'classes_instance.py', 'class_attributes.py', 'class_attributes1.py', 'code_gmplot.py', 'config.py', 'data1.json', 'datafile.txt', 'datawork', 'data_pandas1.csv', 'data_pandas1.xlsx', 'debugger_pdb.py', 'debugging_timeit1.py', 'debugging_timeit2.py', 'define_class.py', 'directoryTreeStruc.py', 'dynamic_array_implementation.py', 'EDA_python1.py', 'EmpID.pickle', 'encapsulation.py', 'encapsulation1.py', 'enumerate1.py', 'eRecord.yaml', 'exampleCSV.csv', 'exampleCSV.py', 'exception1.py', 'exception2.py', 'exception2_1.py', 'exception3.py', 'exception3_1.py', 'exception4.py', 'exception5.py', 'exercise.txt', 'faking_files.py', 'fileone', 'files_background.py', 'finally.txt', 'finally_try_except.py', 'finally_try_except1.py', 'finally_try_except2.py', 'flatten&Ravel_Numpy1.py', 'functions_are_object_too.py', 'function_annotation.py', 'function_annotation1.py', 'function_annotation2.py', 'function_annotation3.py', 'function_annotation4.py', 'func_method.py', 'gc1.py', 'gmplot.py', 'gmplot1.py', 'gmplot11.py', 'gmplot2.py', 'google_search1.py', 'google_search_using_python.py', 'inheritance_example.py', 'inheriting from built-in.py', 'inheriting from built-in1.py', 'inheriting_attributes.py', 'inheriting_the_constructor.py', 'instance_data.py', 'instance_methods.py', 'interques1.py', 'invoice_file1.yaml', 'iterables1.py', 'iterables2.py', 'johnde_test1', 'jsonToPython.py', 'json_example1.json', 'json_example1.py', 'json_example2.py', 'json_script1_loadeRecord.py', 'LDE_EQUITIES_LAST_5_YEARS.csv', 'lib_request.py', 'LICENSE.txt', 'listing_files_directories.py', 'list_comprehension1.py', 'list_comprehension2.py', 'logging.log', 'logging1.py', 'magicmethods_operator_add.py', 'magicmethods_operator_add1.py', 'MainP.py', 'matplotlibsam1.py', 'metaclass1.py', 'metaclass2.py', 'metaclass3.py', 'metaclass4.py', 'method.py', 'methodOverloading.py', 'methodOverloading1.py', 'methodOverloading_defaultArgument.py', 'multiple_inheritance.py', 'multiple_inheritance1.py', 'myfile.py', 'my_map.html', 'NegativeAgeException.py', 'NegativeNumberException.py', 'NEWS.txt', 'object_lookup.py', 'OtherP.py', 'out.txt', 'pandas_script.py', 'pandas_script1.py', 'pandas_script2.py', 'pattern_matching1.py', 'pattern_matching2.py', 'pdb_example1.py', 'pdb_example2.py', 'pickle1.py', 'pickle2.py', 'pickled_list', 'pickle_dictionary1.py', 'pickle_list1.py', 'pickle_test1.py', 'placing_it_in_context.py', 'plotly.py', 'polymorphism_example.py', 'primeNum1.py', 'privateVar1.py', 'private_variable_naming.py', 'project_scrap1.py', 'pygame_script1.py', 'pygmap1.py', 'pygmap2.py', 'pygmaps.py', 'pymaps1.py', 'python.exe', 'python.pdb', 'python3.dll', 'python36.dll', 'python36.pdb', 'pythonw.exe', 'pythonw.pdb', 'python_tricks1.py', 'python_tricks2.py', 'replacing_string_to_number1.py', 'serialization web objects encoder.py', 'serialization web objects.py', 'serialization_JSON.py', 'serialization_pickle.py', 'serialization_pickle1.py', 'serialization_pickle2.py', 'serialization_pickle_storing_instances.py', 'serialization_pyaml_file1.py', 'serialization_pyaml_file2.py', 'serialization_pyaml_script1.py', 'serialization_unpickle.py', 'serialization_unpickle2.py', 'sets1.py', 'simpy1.py', 'simpy2.py', 'stocks_list.csv', 'storing_object1.py', 'storing_objects.py', 'termcolor1.py', 'test.py', 'test.txt', 'test1.py', 'test1.txt', 'test123.py', 'test2.json', 'test2.py', 'test2.txt', 'test_project1', 'test_sample1.py', 'test_sample2.py', 'test_sample3.py', 'test_sample4.py', 'tkinter1.py', 'tkinter2.py', 'tkinter_firstApp.py', 'try_except_block.py', 'tuple1.py', 'unpickle.py', 'unpickle3.py', 'unpickle_dict1.py', 'unpickle_list1.py', 'variable_arguments_list.py', 'variable_arguments_list1.py', 'vcruntime140.dll', 'winquality1.py', 'workfile1', '__init__ Constructor.py']

To get the list of directories using filter:

list(filter(lambda x: os.path.isdir(x), os.listdir('.')))

Output

['.pytest_cache', 'build', 'dist', 'django', 'DLLs', 'Doc', 'etc', 'gmplot', 'gmplot-1.2.0', 'gmplot.egg-info', 'include', 'Lib', 'libs', 'networkP', 'Scripts', 'selenium', 'share', 'tcl', 'Tools', '__pycache__']

Finding files of a particular type

Below is a one-liner to find text files in a directory. Please note this does not descend into directory hierarchy but will just return the matching entries in the specified directory.

list(filter(lambda x: x.endswith('.txt'), os.listdir('.')))

Output

['asyncWrite.txt', 'datafile.txt', 'exercise.txt', 'finally.txt', 'LICENSE.txt', 'NEWS.txt', 'out.txt', 'test.txt', 'test1.txt', 'test2.txt']

We can write above code using list comprehension too,

>>> list(x for x in os.listdir('.') if x.endswith('.txt'))
['asyncWrite.txt', 'datafile.txt', 'exercise.txt', 'finally.txt', 'LICENSE.txt', 'NEWS.txt', 'out.txt', 'test.txt', 'test1.txt', 'test2.txt']

Another way is through regular expression −

import re
fx = re.compile(r'\.(txt|py)')
print(list(filter(fx.search, os.listdir('.'))))

Output

['.pytest_cache', 'annotation1.py', 'asyncWrite.txt', 'attribute_access.py', 'background_process.py', 'background_process2.py', 'BeautifulSoup_script1.py', 'bottle_exampl1.py', 'bottole_test1.py', 'built-in_funct.py', 'callable_objects1.py', 'classes_instance.py', 'class_attributes.py', 'class_attributes1.py', 'code_gmplot.py', 'config.py', 'datafile.txt', 'debugger_pdb.py', 'debugging_timeit1.py', 'debugging_timeit2.py', 'define_class.py', 'directoryTreeStruc.py', 'dynamic_array_implementation.py', 'EDA_python1.py', 'encapsulation.py', 'encapsulation1.py', 'enumerate1.py', 'exampleCSV.py', 'exception1.py', 'exception2.py', 'exception2_1.py', 'exception3.py', 'exception3_1.py', 'exception4.py', 'exception5.py', 'exercise.txt', 'faking_files.py', 'files_background.py', 'finally.txt', 'finally_try_except.py', 'finally_try_except1.py', 'finally_try_except2.py', 'flatten&Ravel_Numpy1.py', 'functions_are_object_too.py', 'function_annotation.py', 'function_annotation1.py', 'function_annotation2.py', 'function_annotation3.py', 'function_annotation4.py', 'func_method.py', 'gc1.py', 'gmplot.py', 'gmplot1.py', 'gmplot11.py', 'gmplot2.py', 'google_search1.py', 'google_search_using_python.py', 'inheritance_example.py', 'inheriting from built-in.py', 'inheriting from built-in1.py', 'inheriting_attributes.py', 'inheriting_the_constructor.py', 'instance_data.py', 'instance_methods.py', 'interques1.py', 'iterables1.py', 'iterables2.py', 'jsonToPython.py', 'json_example1.py', 'json_example2.py', 'json_script1_loadeRecord.py', 'lib_request.py', 'LICENSE.txt', 'listing_files_directories.py', 'list_comprehension1.py', 'list_comprehension2.py', 'logging1.py', 'magicmethods_operator_add.py', 'magicmethods_operator_add1.py', 'MainP.py', 'matplotlibsam1.py', 'metaclass1.py', 'metaclass2.py', 'metaclass3.py', 'metaclass4.py', 'method.py', 'methodOverloading.py', 'methodOverloading1.py', 'methodOverloading_defaultArgument.py', 'multiple_inheritance.py', 'multiple_inheritance1.py', 'myfile.py', 'NegativeAgeException.py', 'NegativeNumberException.py', 'NEWS.txt', 'object_lookup.py', 'OtherP.py', 'out.txt', 'pandas_script.py', 'pandas_script1.py', 'pandas_script2.py', 'pattern_matching1.py', 'pattern_matching2.py', 'pdb_example1.py', 'pdb_example2.py', 'pickle1.py', 'pickle2.py', 'pickle_dictionary1.py', 'pickle_list1.py', 'pickle_test1.py', 'placing_it_in_context.py', 'plotly.py', 'polymorphism_example.py', 'primeNum1.py', 'privateVar1.py', 'private_variable_naming.py', 'project_scrap1.py', 'pygame_script1.py', 'pygmap1.py', 'pygmap2.py', 'pygmaps.py', 'pymaps1.py', 'python_tricks1.py', 'python_tricks2.py', 'replacing_string_to_number1.py', 'serialization web objects encoder.py', 'serialization web objects.py', 'serialization_JSON.py', 'serialization_pickle.py', 'serialization_pickle1.py', 'serialization_pickle2.py', 'serialization_pickle_storing_instances.py', 'serialization_pyaml_file1.py', 'serialization_pyaml_file2.py', 'serialization_pyaml_script1.py', 'serialization_unpickle.py', 'serialization_unpickle2.py', 'sets1.py', 'simpy1.py', 'simpy2.py', 'storing_object1.py', 'storing_objects.py', 'termcolor1.py', 'test.py', 'test.txt', 'test1.py', 'test1.txt', 'test123.py', 'test2.py', 'test2.txt', 'test_sample1.py', 'test_sample2.py', 'test_sample3.py', 'test_sample4.py', 'tkinter1.py', 'tkinter2.py', 'tkinter_firstApp.py', 'try_except_block.py', 'tuple1.py', 'unpickle.py', 'unpickle3.py', 'unpickle_dict1.py', 'unpickle_list1.py', 'variable_arguments_list.py', 'variable_arguments_list1.py', 'winquality1.py', '__init__ Constructor.py']

Using os.walk()- recursive descent

The os.walk() method generates the filenames in a directory tree.

import os
for root, dirs, files in os.walk(r'C:\Python\Python361\selenium'):
   for filename in files:
      print(filename

Output

geckodriver.log
test1.py
x_ignore_nofocus.so
x_ignore_nofocus.so
getAttribute.js
isDisplayed.js
raja
Published on 20-Feb-2019 08:25:06
Advertisements