How to bulk rename PDFs with Python

Today I will show you a simple way to bulk rename your pdf files. If you have a group of PDFs that need to be copied and renamed for multiple folders this script will help you a lot.

Let’s jump to the code.

import os
import shutil
from datetime i…


This content originally appeared on DEV Community and was authored by Stokry

Today I will show you a simple way to bulk rename your pdf files. If you have a group of PDFs that need to be copied and renamed for multiple folders this script will help you a lot.

Let's jump to the code.

import os
import shutil
from datetime import date
from os import walk

after importing we need to define the path:

mypath = r'C:\Users\Stokry\Desktop\python\bulk\main'

the we need to get files to rename:

_, _, filesnames = next(walk(mypath))

files will be copied into folders name John and Anna:

list = ['John', 'Anna']

also, we will set a current date

today = date.today().strftime("%m_%d_%y")

then we can create the new folders:

for name in list:
    newdir = name + '_' + today
    path = os.path.join(mypath, newdir)
    os.mkdir(path)

this will copy the files:

for file in filesnames:
        shutil.copy(mypath + '\\' + file, path)
        os.rename(path + '\\' + file, path + '\\' + name + '' + file)

This is our final result:

enter image description here

Thank you all.


This content originally appeared on DEV Community and was authored by Stokry


Print Share Comment Cite Upload Translate Updates
APA

Stokry | Sciencx (2021-06-10T09:34:34+00:00) How to bulk rename PDFs with Python. Retrieved from https://www.scien.cx/2021/06/10/how-to-bulk-rename-pdfs-with-python/

MLA
" » How to bulk rename PDFs with Python." Stokry | Sciencx - Thursday June 10, 2021, https://www.scien.cx/2021/06/10/how-to-bulk-rename-pdfs-with-python/
HARVARD
Stokry | Sciencx Thursday June 10, 2021 » How to bulk rename PDFs with Python., viewed ,<https://www.scien.cx/2021/06/10/how-to-bulk-rename-pdfs-with-python/>
VANCOUVER
Stokry | Sciencx - » How to bulk rename PDFs with Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/10/how-to-bulk-rename-pdfs-with-python/
CHICAGO
" » How to bulk rename PDFs with Python." Stokry | Sciencx - Accessed . https://www.scien.cx/2021/06/10/how-to-bulk-rename-pdfs-with-python/
IEEE
" » How to bulk rename PDFs with Python." Stokry | Sciencx [Online]. Available: https://www.scien.cx/2021/06/10/how-to-bulk-rename-pdfs-with-python/. [Accessed: ]
rf:citation
» How to bulk rename PDFs with Python | Stokry | Sciencx | https://www.scien.cx/2021/06/10/how-to-bulk-rename-pdfs-with-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.