在Python中重命名目录中的多个文件[重复]

问题描述 投票:391回答:15

这个问题在这里已有答案:

我正在尝试使用Python重命名目录中的一些文件。

假设我有一个名为CHEESE_CHEESE_TYPE.***的文件,并想要删除CHEESE_所以我的结果文件名将是CHEESE_TYPE

我正在尝试使用os.path.split,但它无法正常工作。我也考虑过使用字符串操作,但也没有成功。

python file-io file-rename
15个回答
621
投票

使用os.rename(src, dst)重命名或移动文件或目录。

$ ls
cheese_cheese_type.bar  cheese_cheese_type.foo
$ python
>>> import os
>>> for filename in os.listdir("."):
...  if filename.startswith("cheese_"):
...    os.rename(filename, filename[7:])
... 
>>> 
$ ls
cheese_type.bar  cheese_type.foo

3
投票

此命令将使用renamer从当前目录中的所有文件中删除初始“CHEESE_”字符串:

$ renamer --find "/^CHEESE_/" *

2
投票

您可以使用os.system函数来简化并调用bash来完成任务:

import os
os.system('mv old_filename new_filename')

2
投票

我最初在寻找一些GUI,它允许使用正则表达式重命名,并在应用更改之前预览结果。

在Linux上我已成功使用krename,在Windows上Total Commander使用正则表达式进行重命名,但我发现OSX没有相当的免费等价物,所以我最终编写了一个递归工作的python脚本,默认情况下只打印新文件名而不进行任何操作变化。添加'-w'开关以实际修改文件名。

#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import fnmatch
import sys
import shutil
import re


def usage():
    print """
Usage:
        %s <work_dir> <search_regex> <replace_regex> [-w|--write]

        By default no changes are made, add '-w' or '--write' as last arg to actually rename files
        after you have previewed the result.
        """ % (os.path.basename(sys.argv[0]))


def rename_files(directory, search_pattern, replace_pattern, write_changes=False):

    pattern_old = re.compile(search_pattern)

    for path, dirs, files in os.walk(os.path.abspath(directory)):

        for filename in fnmatch.filter(files, "*.*"):

            if pattern_old.findall(filename):
                new_name = pattern_old.sub(replace_pattern, filename)

                filepath_old = os.path.join(path, filename)
                filepath_new = os.path.join(path, new_name)

                if not filepath_new:
                    print 'Replacement regex {} returns empty value! Skipping'.format(replace_pattern)
                    continue

                print new_name

                if write_changes:
                    shutil.move(filepath_old, filepath_new)
            else:
                print 'Name [{}] does not match search regex [{}]'.format(filename, search_pattern)

if __name__ == '__main__':
    if len(sys.argv) < 4:
        usage()
        sys.exit(-1)

    work_dir = sys.argv[1]
    search_regex = sys.argv[2]
    replace_regex = sys.argv[3]
    write_changes = (len(sys.argv) > 4) and sys.argv[4].lower() in ['--write', '-w']
    rename_files(work_dir, search_regex, replace_regex, write_changes)

Example use case

我想以下列方式翻转文件名的一部分,即将位m7-08移动到文件名的开头:

# Before:
Summary-building-mobile-apps-ionic-framework-angularjs-m7-08.mp4

# After:
m7-08_Summary-building-mobile-apps-ionic-framework-angularjs.mp4

这将执行空运行,并打印新文件名而不实际重命名任何文件:

rename_files_regex.py . "([^\.]+?)-(m\\d+-\\d+)" "\\2_\\1"

这将进行实际重命名(您可以使用-w--write):

rename_files_regex.py . "([^\.]+?)-(m\\d+-\\d+)" "\\2_\\1" --write

2
投票

import os import string def rename_files():

#List all files in the directory
file_list = os.listdir("/Users/tedfuller/Desktop/prank/")
print(file_list)

#Change current working directory and print out it's location
working_location = os.chdir("/Users/tedfuller/Desktop/prank/")
working_location = os.getcwd()
print(working_location)

#Rename all the files in that directory
for file_name in file_list:
    os.rename(file_name, file_name.translate(str.maketrans("","",string.digits)))

rename_files()


0
投票

那这个呢 :

import re
p = re.compile(r'_')
p.split(filename, 1) #where filename is CHEESE_CHEESE_TYPE.***

0
投票

这适合我。

import os
for afile in os.listdir('.'):
    filename, file_extension = os.path.splitext(afile)
    if not file_extension == '.xyz':
        os.rename(afile, filename + '.abc')

36
投票

这是基于您最新评论的脚本。

#!/usr/bin/env python
from os import rename, listdir

badprefix = "cheese_"
fnames = listdir('.')

for fname in fnames:
    if fname.startswith(badprefix*2):
        rename(fname, fname.replace(badprefix, '', 1))

17
投票

以下代码应该有效。它接受当前目录中的每个文件名,如果文件名包含模式CHEESE_CHEESE_,则重命名。如果没有对文件名做任何事情。

import os
for fileName in os.listdir("."):
    os.rename(fileName, fileName.replace("CHEESE_CHEESE_", "CHEESE_"))

15
投票

假设您已经在目录中,并且评论中的“前8个字符”始终为true。 (虽然“CHEESE_”是7个字符......?如果是这样,将下面的8改为7)

from glob import glob
from os import rename
for fname in glob('*.prj'):
    rename(fname, fname[8:])

11
投票

我有同样的问题,我想在任何pdf文件中将空格替换为破折号-。但文件位于多个子目录中。所以,我不得不使用os.walk()。对于多个子目录,可能是这样的:

import os
for dpath, dnames, fnames in os.walk('/path/to/directory'):
    for f in fnames:
        os.chdir(dpath)
        if f.startswith('cheese_'):
            os.rename(f, f.replace('cheese_', ''))

7
投票

试试这个:

import os
import shutil

for file in os.listdir(dirpath):
    newfile = os.path.join(dirpath, file.split("_",1)[1])
    shutil.move(os.path.join(dirpath,file),newfile)

我假设您不想删除文件扩展名,但您可以使用句点进行相同的拆分。


6
投票

这种东西非常适合IPython,它具有shell集成。

In [1] files = !ls
In [2] for f in files:
           newname = process_filename(f)
           mv $f $newname

注意:要将其存储在脚本中,请使用.ipy扩展,并使用!为所有shell命令添加前缀。

另见:http://ipython.org/ipython-doc/stable/interactive/shell.html


3
投票

看来你的问题更多的是确定新文件名而不是重命名本身(你可以使用os.rename方法)。

从您的问题中不清楚您想要重命名的模式是什么。字符串操作没有任何问题。正则表达式可能就是您需要的。


3
投票

这是一个更通用的解决方案:

此代码可用于从目录中的所有文件名中递归删除任何特定字符或字符集,并将其替换为任何其他字符,字符集或无字符。

import os

paths = (os.path.join(root, filename)
        for root, _, filenames in os.walk('C:\FolderName')
        for filename in filenames)

for path in paths:
    # the '#' in the example below will be replaced by the '-' in the filenames in the directory
    newname = path.replace('#', '-')
    if newname != path:
        os.rename(path, newname)
© www.soinside.com 2019 - 2024. All rights reserved.