简体   繁体   中英

Python changing filenames of multiple files in a folder

I'm trying to make a script to rename all files in a folder and this is the code I have, but it doesn't work. I have like 200 images in that folder and I want to change filename1_ to filename1 and so on for all the files. The transformation is dog_1 to doggy1; or whatever, the new name of the file can be anything. Could somebody help me out? I'm still new to python so the code I have right now might look like nothing.

import os
    directoryname="C:\\Users\\Ineke\\Documents\\Python Scripts\\images"
    lijstmetfiles = os.listdir(directoryname)
    for i in range(len(lijstmetfiles)):
        os.rename(str(lijstmetfiles[i]), str("doggy"+str(i))

I ran your code and here are your two main problems:

The first being, you are not closing your bracket on your os.rename line.

You have this:

os.rename(str(lijstmetfiles[i]), str("doggy"+str(i))

You are missing a parentheses, so should be:

os.rename(str(lijstmetfiles[i]), str("doggy"+str(i)))

However, this could be an edit issue from copying your code over in to your post.

Secondly, and most importantly, you are not specifying the path you want to do your rename on, you are just giving two filenames, so you are most likely getting a file not found error.

You need to make use of Python's os.path.join method using your directory and the filename, as in the code sample below:

os.rename(
    os.path.join(directoryname, str(lijstmetfiles[i])),
    os.path.join(directoryname, str("doggy"+str(i)))
    ) 

So now you are explicitly specifying what the full path is for your rename.

Another point to make, you don't need to be casting to a "str" for your filenames. Even if your filename is 5 for example, getting the list of files from listdir will still come back as a string.

Finally, putting it all together, your code should look something like this:

import os
directoryname="C:\\Users\\Ineke\\Documents\\Python Scripts\\images"
lijstmetfiles = os.listdir(directoryname)
print(lijstmetfiles)
for i in range(len(lijstmetfiles)):
    os.rename(
        os.path.join(directoryname, lijstmetfiles[i]),
        os.path.join(directoryname, "doggy"+str(i))
        )

I tested this on my end and it should work.

Here is documentation on the os module. Take a look at it to get a further understanding of what is available to you when playing with the filesystem:

Python 2: https://docs.python.org/2/library/os.html

Python 3: https://docs.python.org/3/library/os.html

The following code will rename all of the files in that directory with similar names but with _ excluded.

#UNTESTED
import os
directoryname="C:\\Users\\Ineke\\Documents\\Python Scripts\\images"
lijstmetfiles = os.listdir(directoryname)
for i in lijstmetfiles:
    os.rename(os.path.join(directoryname, i),
              os.path.join(directoryname,
                           i.replace('_','')))

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