Project Level 3: Real-World
This project is designed for learners who know Python fundamentals and are learning to build real-world programs.
Project Description
his Python command-line tool extracts metadata from files in a specified directory and displays it. Metadata includes the file size, creation date, modification date, and file type. Users can see a list of all files in a directory, including files in subdirectories, along with their metadata. The results can also be saved into a CSV file for later use or analysis.
How the program works
The program lets the user enter a directory path in the terminal where some files are located (e.g., /Users/as/Downloads/myfolder for Mac/Linux or C:\Users\as\Downloads\myfolder for Windows):
The program displays the list of the filepaths contained in the given directory and their metadata (size, creation date, and type):
The program also lets the user choose whether to save the metadata results in a CSV file or not:
Here is how the metadata look in the CSV file:
Tip: You can extract the file creation time and size using the os library:
creation_time = os.path.getctime(file_path)
modification_time = os.path.getmtime(file_path)
Learning Benefits
Learn how to work with file metadata using Python's
os
anddatetime
modules.Understand how to display and organize file information in a structured format.
Practice saving information into CSV files using Python’s
csv
module.
Prerequisites
Required Libraries: os, csv, datetime
You don’t need to install any libraries since all the libraries are standard Python libraries.
Required Files: No files are required.
IDE: Use any IDE.
Danger Zone
The solution code is hidden behind the button below. Click to reveal it.
Happy Coding!
Daily Python Projects Team
HI,
is my version of code below correct?
import csv
import os
import datetime
dir_path = input("Enter the directory path to scan: ")
metadata_list = []
for filename in os.listdir(dir_path):
file_path = os.path.join(dir_path, filename)
if os.path.isfile(file_path):
size = os.path.getsize(file_path)
created_timestamp = os.path.getctime(file_path)
modified_timestamp = os.path.getmtime(file_path)
created_datetime = datetime.datetime.fromtimestamp(created_timestamp)
modified_datetime = datetime.datetime.fromtimestamp(modified_timestamp)
file_type = os.path.splitext(file_path)[1]
print(f"Path: {file_path}, Size: {size}, Created: {created_datetime}, Modified: {modified_datetime}, "
f"Type: {file_type}")
metadata_list.append([file_path, size, created_datetime, modified_datetime, file_type])
save_to_csv = input("Do you want to save the metadata to a CSV file? (Y/N): ")
if save_to_csv.lower() == "y":
csv_filename = "file_metadata_list.csv"
with open(csv_filename, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["Path", "Size (bytes)", "Created", "Modified", "Type"])
writer.writerows(metadata_list)
print(f"File metadata saved to {csv_filename}")
elif save_to_csv.lower() != 'n':
print("Invalid input, File metadata not saved.")