简体   繁体   中英

shutil.move removing file extensions

You guys have been a great help with my learning python. I was tasked today with modifying an old employees code and have since downloaded several resources to help. I am renaming files in a folder. Basically a user inputs something in a text box and that is being added on to the end of the file.

if len(self.toLoc.get()) == 0:
        searchRev = "_R" + newRev 
        for filename in os.listdir(App.pdfDir):
            sep = searchesri
            rest = filename.split(sep, 1)[0] + searchRev + fromLocation
            shutil.move(os.path.join(App.pdfDir, filename), os.path.join(App.pdfDir, rest))

it works perfectly, the only issue i have is that it does not keep the file extensions and unfortunatly there are pdfs and xlsx's in the dir so I can't just hard code pdf on the end. I tried to see if the os function had a way of getting it but i kind of confused myself...

I'm not sure what searchesri is, but you're throwing away part of the file name in filename.split(sep, 1)[0] . Maybe you need to append filename.split(sep, 1)[1] to the end of your destination file name?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM