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

编程入门 行业动态 更新时间:2024-10-07 08:19:41
Python,如何根据列表重命名几个文件?(Python, how can I rename several files based off a list?)

在Windows上使用python尝试重命名同一个文件夹中的多个文件,但是我无法使用列表进行重命名,这就是我在尝试使用代码时出现此错误的原因:

os.rename(dirlist [1],单词[1])WindowsError:[错误2]系统找不到指定的文件

以下是示例代码:

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脚本在选择的文件夹上运行,脚本将获取其中的所有文件并将重命名所有文件。 但是当我无法挑出文件夹名称并重命名它们因为它们附加到列表时,棘手的部分就出现了。

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.

最满意答案

os.listdir只返回基本名称结果。 不完整的路径。 它们不存在于您当前的工作目录中。 您需要将它们与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'))

更新

在回答您关于如何执行大量替换的评论时,我建议您可以使用translate和maketrans 。

让我们从我们的dict和源字符串开始:

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

首先,让我向您展示一个非常原始的入门级方法的示例:

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

该示例循环遍历您的字典,多次调用替换。 它很有效,使用简单的语法也很有意义。 但是,为每个字符串循环遍历字典并多次调用替换它有点糟糕。

我确信还有很多其他方法可以做到这一点,但另一种方法是创建一次转换表,然后为每个字符串重用它:

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

这将把字典拆分为键和值元组。 然后我们加入intro字符串并将它们传递给maketrans ,这将给我们一张桌子。 我们只需要这样做一次。 现在我们有一个表,可以用它来翻译任何字符串。

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.

更多推荐

本文发布于:2023-08-04 00:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1402422.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:几个   重命名   文件   列表   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!