How to avoid overwriting files in python

Strategy 1: increment a suffix so like “file.pdf(1)”

Version: Simple

Python
import os

def get_unique_filename(file_path):
    base, extension = os.path.splitext(file_path)
    counter = 1
    while os.path.exists(file_path):
        file_path = f"{base}({counter}){extension}"
        counter += 1
    return file_path

original_file_path = "/tmp/files/myfile.pdf"
unique_file_path = get_unique_filename(original_file_path)

# Now you can write to `unique_file_path` without overwriting existing files
data_to_write = "This is the new file content.\n"
with open(unique_file_path, 'w') as file:
    file.write(data_to_write)

print(f"File written to {unique_file_path}")

Version: Guards against race conditions

The above is safe if there is only one process writing to the location but it can include a race condition. This is the safer version that “opens” the file.

Python
import os

def create_unique_file(base_path, data):
    # Check if the original file already exists
    if not os.path.exists(base_path):
        # If it doesn't exist, try to create it
        try:
            with open(base_path, 'x') as file:
                file.write(data)
            print(f"File created successfully: {base_path}")
            return
        except FileExistsError:
            # If the file was created between the existence check and the attempt to open it
            # Proceed to the incrementing logic
            pass  

    base, extension = os.path.splitext(base_path)
    counter = 1

    while True:
        # Construct the file path with a counter
        file_path = f"{base}({counter}){extension}"
        try:
            with open(file_path, 'x') as file:
                file.write(data)
            print(f"File created successfully: {file_path}")
            break  # Exit the loop if the file was created successfully
        except FileExistsError:
            counter += 1  # Increment the counter and try again

# Usage example
original_file_path = "/tmp/files/myfile.pdf"
data_to_write = "This is the content of the file.\n"
create_unique_file(original_file_path, data_to_write)

Strategy 2: attach a timestamp

  • this is same if you are the only one writing to the folder
Python
import os
from datetime import datetime

def get_timestamped_filename(file_path):
    base, extension = os.path.splitext(file_path)
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    return f"{base}_{timestamp}{extension}"

original_file_path = "/tmp/files/myfile.pdf"
timestamped_file_path = get_timestamped_filename(original_file_path)

# Now you can write to `timestamped_file_path` without overwriting existing files
data_to_write = "This is the new file content.\n"
with open(timestamped_file_path, 'w') as file:
    file.write(data_to_write)

print(f"File written to {timestamped_file_path}")