Skip to content

Instantly share code, notes, and snippets.

@bradmartin333
Created January 15, 2024 16:38
Show Gist options
  • Save bradmartin333/0a56d6f3516d5f09bbe407bf5f316e18 to your computer and use it in GitHub Desktop.
Save bradmartin333/0a56d6f3516d5f09bbe407bf5f316e18 to your computer and use it in GitHub Desktop.
rename PDFs easily
import os
import subprocess
def rename_pdfs(directory):
"""Iterates through PDFs in a directory, opens them, and renames them with user input."""
pdf_files = [f for f in os.listdir(directory) if f.endswith(".pdf")]
for filename in pdf_files:
pdf_path = os.path.join(directory, filename)
# Open the PDF in the default app
subprocess.Popen([pdf_path], shell=True) # Use the new path after renaming
# Prompt for the new filename before opening the PDF
new_filename = input(
"Enter new filename for '{}' (without extension): ".format(filename)
)
# Rename the file
new_filename = new_filename + ".pdf"
new_path = os.path.join(directory, new_filename)
os.rename(pdf_path, new_path)
if __name__ == "__main__":
directory = input("Enter the directory to process: ")
rename_pdfs(directory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment