Python Forum
rename many pdf'S in a directory - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: rename many pdf'S in a directory (/thread-16403.html)



rename many pdf'S in a directory - deep_logic - Feb-26-2019

Hi all,
I'm trying various ways to rename a bunch of pdf's in a directory. I have autocad batch print 50 files to adobe pdf. The problem is the default filename will add "Layout (1) .pdf" at the end of the filename. Just to be tidy, I'd like to remove the "Layout (1) .pdf" and just keep the original file name that autocad has.

This is what I have so far:
###Script to remove Layout (1)
###from the filename that gets
###generated from autocad and
###adobe pdf batch plots - LAME!
###
###deep_logic 02/26/19
###
import os, sys, re, glob

from glob import glob
from os import rename

for fileName in os.listdir(r'c:\\TESTZ'):
    os.rename(fileName, fileName.replace("Layout (1)", "__"))
The filename length does change - that's why it's not as easy as using dict or a variation thereof.

The code doesn't do ANYTHING - it just hangs. So, if anyone could help a coder out...

I'm on python 2.7 and windows 10.

Many thanks


RE: rename many pdf'S in a directory - snippsat - Feb-27-2019

(Feb-26-2019, 07:30 PM)deep_logic Wrote: I have autocad batch print 50 files to adobe pdf. The problem is the default filename will add "Layout (1) .pdf" at the end of the filename.
How dos the raw filename look?
Let say as you say just add fooLayout (1).pdf hello worldLayout (2).pdf
import os
import re

for file_name in os.listdir('.'):
    if file_name.endswith('.pdf'):
        new_name = re.sub(r'L.*\)', '', file_name)
        # Always test print before doing chages to files
        print(file_name, new_name)
        #os.rename(file_name, new_name)
Output:
foo.pdf hello world.pdf



RE: rename many pdf'S in a directory - ddrillic - Feb-27-2019

Gorgeous thing! it works like a charm.


RE: rename many pdf'S in a directory - deep_logic - Feb-28-2019

snippsat,

dood...many thanks! I finally got it to go! Much manna to you!

Thanks again!