GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||
GeezerVison is showing zetcode.com — Python list directory - listing directory contents in Python Python list directorylast modified January 29, 2024 Python list directory tutorial shows how to list directory contents in Python. Multiple examples show how to list the directory contents and filter it. Directory definitionDirectory is an organizing unit in a computer's file system for storing and locating files. Directories are hierarchically organized into a tree of directories. Directories have parent-child relationships. A directory is sometimes also called a folder.
Over the years, Python gained multiple APIs to list the directory contents.
For instance, we can use the Python Path.iterdir
The listing.py
#!/usr/bin/python
from pathlib import Path
path = '.'
for path in Path(path).iterdir():
print(path)
The example lists the contents of the current directory. The listing is non-recursive and includes both files and directories. $ ./listing.py recursive_glob.py list_dir_files2.py multiple_extensions.py walking3.py list_files.py temperatures.csv list_pandas.py src ... Python Path.iterdir list directoriesThe following example shows how to list only directories. list_dirs.py
#!/usr/bin/python
from pathlib import Path
path = Path('.')
dirs = [e for e in path.iterdir() if e.is_dir()]
for dir in dirs:
print(dir)
#print(dir.parts[-1])
The example shows all immediate subdirectories of the current directory. dirs = [e for e in path.iterdir() if e.is_dir()]
We build a list of directories using Python list comprehension.
The for dir in dirs:
print(dir)
#print(dir.parts[-1])
In a for loop, we print all the directories we have found. We can display the
whole path or use the Python Path.iterdir list filesThe following example shows how to list only files. list_files.py
#!/usr/bin/python
from pathlib import Path
path = Path('/home/janbodnar/Documents/prog/python/')
files = [e for e in path.iterdir() if e.is_file()]
for file in files:
print(file)
The example lists all files in the given directory. We use the Python Path.iterdir succint exampleThe following example shows a very succint code example. list_files.py
#!/usr/bin/python from pathlib import Path home_path = Path.home() print(*Path(home_path).iterdir(), sep="\n")
The Python list directory with Path.glob
The globbing.py
#!/usr/bin/python
from pathlib import Path
path = Path('.')
for e in path.glob('*.py'):
print(e)
The example prints all files which end in $ ./globbing.py recursive_glob.py list_dir_files2.py multiple_extensions.py walking3.py list_files.py list_pandas.py list_files_modified.py recursive_scandir.py list_dir_files.py ... Python list directory recursively with Path.rglob
The rglobbing.py
#!/usr/bin/python
from pathlib import Path
path = Path('/home/janbodnar/Documents/prog/python/')
for e in path.rglob('*.py'):
print(e)
# for e in path.glob('**/*.py'):
# print(e)
The example finds all Python files in the given directory and all its subdirectories. The commented code does the same job. Python list directory with multiple extensionsThe following example lists all files with three possible extensions. multiple_extensions.py
#!/usr/bin/python
from pathlib import Path
path = Path.cwd()
files = { p.resolve() for p in Path(path).glob("**/*")
if p.suffix in ['.py', '.csv', '.txt'] }
for file in files:
print(file.parts[-1])
The example lists all files = { p.resolve() for p in Path(path).glob("**/*")
if p.suffix in ['.py', '.csv', '.txt'] }
We use a set comprehension to build a set of files with the provided extensions. $ ./multiple_extensions.py data_2019.csv walking.py list_files.py data.txt multiple_extensions.py scan_dir.py list_files_size.py list_pandas.py temperatures.csv recursive_glob.py list_dir_files2.py data.csv ... Python list directory with os.scandir
The
The scan_dir.py
#!/usr/bin/python
import os
path = '.'
with os.scandir(path) as it:
for entry in it:
if entry.is_file():
print(entry.name)
The example list all files in the current working directory. Python list directory recursively with os.walk
The walking.py
#!/usr/bin/python
import os
path = '/home/janbodnar/Documents/prog/python/'
for dirpath, dirs, files in os.walk(path):
for file in files:
file_name = os.path.join(dirpath, file)
if file_name.endswith('.py'):
print(file_name)
The example lists all Python files. To check if a file is a Python file, we
use the Python list directory recursively with os.scandir
The recursive_scandir.py
#!/usr/bin/python
import os
def scan_recursive(path):
with os.scandir(path) as it:
for entry in it:
if entry.is_file():
yield entry.name
else:
yield from scan_recursive(entry.path)
for e in scan_recursive('/root/Documents/prog/python/'):
print(e)
The example uses the Python Path treeThe following practical program outputs the contents of the specified directory in a hierarchical tree structure. path_tree.py
#!/usr/bin/python
from pathlib import Path
def generate_tree(directory):
print(f'+ {directory}')
for path in sorted(directory.rglob('*')):
depth = len(path.relative_to(directory).parts)
spacer = ' ' * depth
# print(f'{spacer}+ {path.name}')
if path.is_file():
print(f'{spacer}f {path.name}')
else:
print(f'{spacer}d {path.name}')
path = Path.home() / 'Downloads'
generate_tree(path)
The example generates a directory tree from the Downloads directory. Python list directory files by sizeThe following example lists files filtered by size. list_files_size.py
#!/usr/bin/python
import os
from pathlib import Path
path = Path.home() / 'Downloads'
size = 1024*1024*1024
large_files = [e for e in path.rglob('*.*') if e.is_file() and os.path.getsize(e) >= size]
for file in large_files:
print(f'{file} {os.path.getsize(file)}')
The example prints all files that are greater than 1GB in the Downloads
directory. We use the Python list directory by modification dateThe following example lists all files by modification date. list_files_modified.py
#!/usr/bin/python
import os
import datetime
now = datetime.datetime.now()
ago = now - datetime.timedelta(minutes=30)
path = '/home/janbodnar/Documents/prog/python/'
for root, dirs, files in os.walk(path):
for fname in files:
path = os.path.join(root, fname)
st = os.stat(path)
mtime = datetime.datetime.fromtimestamp(st.st_mtime)
if mtime > ago:
print(f'{path} modified {mtime}')
In the example, we list all files that have been modified in the last 30 minutes.
We use the Python list directory with os.listdir
The This is older API and it is generally recommended to use more recent APIs for directory listings. list_dir.py
#!/usr/bin/python
import os
path = '.'
files = os.listdir(path)
for file in files:
print(file)
The example lists all files and directories in the current directory. list_files2.py
#!/usr/bin/python import os dirs = filter(os.path.isfile, os.listdir()) print(tuple(dirs))
In this example, we list all files in the current working directory.
We use the built-in Python fnmatch with os.listdir
The
Table: The
fnmatch special characters
The list_csv.py
#!/usr/bin/python
import fnmatch
import os
path = '.'
for file in os.listdir(path):
if fnmatch.fnmatch(file, '*.csv'):
print(file)
In the example, we list all CSV files. $ ./list_csv.py temperatures.csv data_2019.csv data.csv We have three CSV files in the directory. Python directory contents in table with PandasPandas is a Python data analysis and manipulation tool. The following example shows the directory listings in a Pandas DataFrame. list_pandas.py
#!/usr/bin/python
import pandas as pd
from pathlib import Path
import time
path = Path.cwd()
all_files = []
for e in path.rglob('*.*'):
all_files.append((e.name, e.parent, time.ctime(e.stat().st_ctime)))
columns = ['File_Name', 'Parent', 'Created']
df = pd.DataFrame.from_records(all_files, columns=columns)
print(df.head(5))
In the example, we show file names, their suffix and creation date for the first file entries in a Pandas DataFrame. $./list_pandas.py File name Suffix Created 0 recursive_glob.py .py Fri Sep 11 13:04:54 2020 1 list_dir_files2.py .py Fri Sep 11 12:50:22 2020 2 multiple_extensions.py .py Fri Sep 11 16:09:30 2020 3 walking3.py .py Fri Sep 11 13:06:31 2020 4 list_files.py .py Fri Sep 11 14:23:47 2020 Python directory contents in table with PrettyTablePrettyTable is a Python library for generating simple ASCII tables. list_pretty_table.py
#!/usr/bin/python
from prettytable import PrettyTable
from pathlib import Path
import time
pt = PrettyTable()
path = Path.cwd()
all_files = []
pt.field_names = ['File name', 'Suffix', 'Created']
for e in path.rglob('*.*'):
pt.add_row((e.name, e.suffix, time.ctime(e.stat().st_ctime)))
print(pt)
We show the file names of the current directory, their suffix and creation date in a table generated by PrettyTable. $ ./list_pretty_table.py +------------------------+--------+--------------------------+ | File name | Suffix | Created | +------------------------+--------+--------------------------+ | recursive_glob.py | .py | Fri Sep 11 13:04:54 2020 | | list_dir_files2.py | .py | Fri Sep 11 12:50:22 2020 | | multiple_extensions.py | .py | Fri Sep 11 16:09:30 2020 | | walking3.py | .py | Fri Sep 11 13:06:31 2020 | | list_files.py | .py | Fri Sep 11 14:23:47 2020 | | temperatures.csv | .csv | Fri Sep 11 12:50:37 2020 | | list_pandas.py | .py | Sat Sep 12 08:11:57 2020 | | list_files_modified.py | .py | Fri Sep 11 14:59:32 2020 | | recursive_scandir.py | .py | Fri Sep 11 16:42:36 2020 | | list_dir_files.py | .py | Fri Sep 11 12:48:49 2020 | | data_2019.csv | .csv | Fri Sep 11 12:47:35 2020 | | listing2.py | .py | Fri Sep 11 15:18:03 2020 | | listing.py | .py | Fri Sep 11 15:12:55 2020 | | system_list.py | .py | Fri Sep 11 12:53:13 2020 | | listing3.py | .py | Fri Sep 11 13:24:58 2020 | | scan_dir.py | .py | Fri Sep 11 16:00:53 2020 | | list_dir.py | .py | Fri Sep 11 12:46:34 2020 | | list_files_size.py | .py | Fri Sep 11 16:52:20 2020 | | walking.py | .py | Fri Sep 11 16:41:27 2020 | | walking2.py | .py | Fri Sep 11 12:46:34 2020 | | list_dir_generator.py | .py | Sat Sep 12 07:54:17 2020 | | scan_dir2.py | .py | Fri Sep 11 16:00:23 2020 | | data.csv | .csv | Fri Sep 11 12:47:35 2020 | | list_files2.py | .py | Fri Sep 11 17:07:18 2020 | | globbing.py | .py | Fri Sep 11 15:26:45 2020 | | rglobbing.py | .py | Fri Sep 11 15:26:24 2020 | | data.txt | .txt | Fri Sep 11 12:47:35 2020 | | path_tree.py | .py | Fri Sep 11 13:26:39 2020 | | list_csv.py | .py | Sat Sep 12 07:48:16 2020 | | links.txt | .txt | Fri Sep 11 16:58:25 2020 | | succint.py | .py | Fri Sep 11 15:49:54 2020 | | list_pretty_table.py | .py | Sat Sep 12 08:13:37 2020 | +------------------------+--------+--------------------------+ SourcePython File and Directory Access - language reference In this article we have showed how to list the directory contents in Python. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. | ||||||||||
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |