Python,如何根据列表重命名几个文件? [英] Python, how can I rename several files based off a list?

查看:553
本文介绍了Python,如何根据列表重命名几个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows Im中使用python尝试一次重命名同一文件夹中的多个文件,但是我无法使用列表进行重命名,这就是为什么在尝试我的代码时会收到此错误的原因:

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:[错误2]系统 找不到指定的文件

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

这是示例代码:

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])

我想要实现的是让我的python脚本在选择的文件夹上运行,该脚本将在其中容纳所有文件,并将所有文件重命名.但是,当我无法挑选出文件夹名称并重新命名它们(因为它们已附加到列表中)时,棘手的部分就来了.

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.

推荐答案

os.listdir仅将基本名称的结果还给您.没有完整的路径.它们在您当前的工作目录中不存在.您需要使用根将它们重新加入:

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'))

更新

针对您对如何进行大量更换的评论,我建议您可以使用translatemaketrans.

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

这会将字典拆分为键和值元组.然后,我们先连接介绍字符串,然后将它们传递给maketrans,这将给我们返回一个表.我们只需要这样做一次.现在我们有了一个表,可以用它来转换任何字符串.

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.

这篇关于Python,如何根据列表重命名几个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆