Menu

Write a file inventory to CSV with Python
📰

Write a file inventory to CSV with Python

DEV Community: python·Prarit Sidana·1 day ago
#M9aji0ae
#devtopython#devto#dev#files#folder
Reading 0:00
15s threshold

AI-assisted article. The file-inventory example was checked with temporary fixtures, including empty folders and filenames that require CSV quoting.

Python automation works well when a repeatable task has a clear input and a result you can check. A file inventory is a practical first exercise: take one folder and produce a table of file names and sizes. The report can help review a download folder or prepare a list of documents before a larger workflow.

Start with a small folder

Create a folder named sample-files and add two small text files. The example below lists files directly inside that folder. It skips subfolders and symbolic links, reads file metadata, and creates a new CSV report. It does not rename, move or edit the source files.

Python's standard library supplies pathlib for working with paths and csv for writing the table. Save this example as inventory.py:

import csv from pathlib import Path

def spreadsheet_text ( value ) : if value .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.