Python · File Utility

List Every File in a Folder to a Text or CSV Manifest with Python

TL;DR

Drop file_lister_v2.py in a folder and run it: you get extracted_files_details.txt with every file name, size and an extension count, as a readable list or CSV.

Updated 1 min read

Pythonos / pathlibCSV

Download source (ZIP)v1.0.0 · 2 files · 5 KB · MIT license

Abstract

The problem

Listing hundreds of assets with their metadata by hand is a time sink, and OS file views do not allow copy-pasting a tidy text list.

The solution

A configurable "edit-and-run" script that scans a directory, filters it, and writes a formatted text or CSV manifest with extension statistics.

The result

Zero-effort file manifests ready for documentation, client reports or database imports.

Input & Output

Input
  • The folder containing file_lister_v2.py (scanned in place)
Output
  • extracted_files_details.txt – numbered list or CSV
Optional output
  • Extension distribution summary (e.g. png: 45, jpg: 12)
  • Human-readable sizes (KB / MB / GB)

System Diagram

How It Works

Self-exclusion

The script hides itself so the list stays clean.

Format targeting

Toggle visibility of specific file types such as .txt or .py.

Two report styles

Numbered human-readable list, or CSV for Excel and databases.

Directory health

Counts every extension and converts raw bytes to readable sizes.

Configuration

OptionPurpose
SORT_BY'name' for alphabetical, 'created' for chronological.
OUTPUT_FORMAT'default' (list) or 'csv'.
OMIT_FILE_SIZETrue for a name-only list, False to include sizes.

How to Run

Copy file_lister_v2.py into the folder to catalogue
# place the script next to your files
Run it
python file_lister_v2.py

Source Code

file_lister_v2.pypreview
import os
import datetime
from collections import defaultdict

# --- CONFIGURATION FLAGS ---
# Set these flags to control the script's behavior.

# Sort files by 'name' (alphabetically) or 'created' (creation time).
SORT_BY = 'name'

# Choose output format: 'default' for a numbered list, 'csv' for plain CSV format.
OUTPUT_FORMAT = 'default'

# Omit the script's own file (.py) from the output.
# Set to 'False' to include the script file.
OMIT_PY_FILES = True

# Omit all text files (.txt) from the output.
# Set to 'False' to include text files.
OMIT_TXT_FILES = True

# Omit the file size from the output.
# Set to 'True' to remove the size information.
OMIT_FILE_SIZE = True

# --------------------------------------------------------------------------

def format_size(size_in_bytes):
    """
    Formats file size into a human-readable string (KB, MB, GB).
    """
    if size_in_bytes < 1024:
        return f"{size_in_bytes} B"
    elif size_in_bytes < 1024 * 1024:
        return f"{size_in_bytes / 1024:.2f} KB"
    elif size_in_bytes < 1024 * 1024 * 1024:
        return f"{size_in_bytes / (1024 * 1024):.2f} MB"
    else:
        return f"{size_in_bytes / (1024 * 1024 * 1024):.2f} GB"

def main():
    """
    This script is designed to list all files in the directory it is run from,
    gather detailed information about them, and save that information to a
    text file named 'extracted_files_details.txt'.

    The script's behavior is configured by the flags at the top of this file,
    making it easy to customize without using command-line arguments.

    How to use:
    1. Place this script in the directory you want to catalog.
    2. Edit the configuration flags above to your desired settings.
    3. Open a terminal or command prompt.
    4. Navigate to the directory where the script is located.
    5. Run the script by typing 'python file_lister_v2.py' and pressing Enter.
    6. A new file, 'extracted_files_details.txt', will be created in the same folder.
    """
    root_folder = os.getcwd()
    output_filename = "extracted_files_details.txt"
    

… 74 more lines - loading the full file

Download source (ZIP)v1.0.0 · 2 files · 5 KB · MIT license

Released under the MIT license. Free to use, modify and share.

Impact & Benefits

Quick asset lists for project hand-offs (100+ UI/UX images).
Table-of-contents generation for source or assets.
Data-entry automation without manual typing.
Space audits by sorting on size to find heavy files.

Screenshots

1/2 The script placed in the folder to catalogue
2/2 Generated output - numbered list with extension summary

Frequently Asked Questions

How do I export a folder listing to CSV with Python?

Set OUTPUT_FORMAT = 'csv' at the top of file_lister_v2.py and run it. The result opens directly in Excel.

Does the script list itself?

No. It excludes itself so the manifest only contains your files.

Can I sort by creation date?

Yes. Set SORT_BY to 'created' for chronological order or 'name' for alphabetical.

Related

© 2026 Thuyavan Sivarasa. Script code is MIT licensed.