Python, how can I rename several files based off a

2019-06-03 10:35发布

问题:

Using python with Windows Im trying to rename several files at once that are in the same folder but I cant use a list to do a rename that is why I get this error when I try my code:

os.rename(dirlist[1], words[1]) WindowsError: [Error 2] The system cannot find the file specified

Here is the sample code:

import os
import sys
words = os.listdir('C:/Users/Any/Desktop/test')
dirlist = os.listdir('C:/Users/Any/Desktop/test')

words = [w.replace('E', 'e') for w in words]
print words 

os.rename(dirlist[1], words[1])

What I am trying to achieve is have my python script ran on a folder of choice and the script will take all the files in there and will rename all of them. But the tricky part comes when I cant single out the folder names and have them renamed because they are attached to the list.

回答1:

os.listdir is only giving you back the basename results. Not full path. They don't exist in your current working directory. You would need to join them back with the root:

root = 'C:/Users/Any/Desktop/test'
for item in os.listdir(root):
    fullpath = os.path.join(root, item)
    os.rename(fullpath, fullpath.replace('E', 'e'))

Update

In response to your comment about how to perform larger number of replacements, I had suggested you could use translate and maketrans.

Let's start with our dict and a source string:

d = {'E': 'e', 'a': 'B', 'v': 'C'}
s = 'aAaAvVvVeEeE'

First, let me show you an example of a very primitive and entry level approach:

for old, new in d.iteritems():
    s = s.replace(old, new)

print s
# BABACVCVeeee

That example loops over your dictionary, calling the replacement multiple times. It works, and it makes perfect sense, using simple syntax. But it kind of sucks having to loop over the dictionary for every string and call replace multiple times.

There are many other ways to do this I am sure, but another approach is to create a translation table once, and then reuse it for every string:

import string

old, new = zip(*d.items())
print old, new
# ('a', 'E', 'v') ('B', 'e', 'C')

old_str, new_str = ''.join(old), ''.join(new)
print old_str, new_str
# aEv BeC

table = string.maketrans(old_str, new_str)

print s.translate(table)
# BABACVCVeeee

That will split the dictionary out to key and value tuples. Then we join then intro strings and pass them to maketrans, which will give us back a table. We only have to do that once. Now we have a table and can use it to translate any string.



回答2:

The error is coming because your current working directory doesn't points to 'C:/Users/Any/Desktop/test', by default it points to c:\python27 if you're using python 2.7, you can check yours by os.getcwd(), so you should change your current working directory to 'C:/Users/Any/Desktop/test' before applying that rename.

use os.chdir() to change the current working directory.

import os
import sys
words = os.listdir('C:/Users/Any/Desktop/test')
dirlist = os.listdir('C:/Users/Any/Desktop/test')

words = [w.replace('E', 'e') for w in words]
print words 

os.chdir('C:/Users/Any/Desktop/test')  #this changes the current working directory
os.rename(dirlist[1], words[1])