批量重命名目录中的文件
有没有一种简单的方法可以使用 Python 重命名目录中已包含的一组文件?
示例:我有一个充满 *.doc 文件的目录,我想以一致的方式重命名它们。
X.doc-> “新(X).doc”
Y.doc-> “新(Y).doc”
Is there an easy way to rename a group of files already contained in a directory, using Python?
Example: I have a directory full of *.doc files and I want to rename them in a consistent way.
X.doc -> "new(X).doc"
Y.doc -> "new(Y).doc"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
如果您想在编辑器(例如 vim)中修改文件名,请单击 库附带命令
click.edit()
,可用于从编辑器接收用户输入。 以下是如何使用它来重构目录中的文件的示例。我编写了一个使用相同技术的命令行应用程序,但这减少了该脚本的波动性,并提供了更多选项,例如递归重构。 以下是 github 页面 的链接。 如果您喜欢命令行应用程序,并且有兴趣对文件名进行一些快速编辑,这非常有用。 (我的应用程序类似于 ranger 中的“bulkrename”命令)。
If you would like to modify file names in an editor (such as vim), the click library comes with the command
click.edit()
, which can be used to receive user input from an editor. Here is an example of how it can be used to refactor files in a directory.I wrote a command line application that uses the same technique, but that reduces the volatility of this script, and comes with more options, such as recursive refactoring. Here is the link to the github page. This is useful if you like command line applications, and are interested in making some quick edits to file names. (My application is similar to the "bulkrename" command found in ranger).
该代码将起作用
该函数精确地采用两个参数 f_path 作为重命名文件的路径,并将 new_name 作为文件的新名称。
This code will work
The function exactly takes two arguments f_patth as your path to rename file and new_name as your new name to the file.
基于 Cesar Canassa 上面评论构建。
这将找到三个下划线 (_) 并将它们及其后面的所有内容替换为空 ('')。
Building off of Cesar Canassa comment above.
This will find three underscores (_) and replace them and everything after them with nothing ('').
我更喜欢为我必须做的每次替换编写一小段代码,而不是编写更通用和复杂的代码。 例如:
这会将当前目录中任何非隐藏文件中的所有下划线替换为连字符
I prefer writing small one liners for each replace I have to do instead of making a more generic and complex code. E.g.:
This replaces all underscores with hyphens in any non-hidden file in the current directory
这种重命名非常简单,例如使用 os 和 glob 模块:
然后您可以在示例中使用它,如下所示:
上面的示例将转换所有
文件复制到c:\temp\xx
目录中的 *.docnew(%s).doc
,其中%s
是文件以前的基本名称(不带扩展名)。Such renaming is quite easy, for example with os and glob modules:
You could then use it in your example like this:
The above example will convert all
*.doc
files inc:\temp\xx
dir tonew(%s).doc
, where%s
is the previous base name of the file (without extension).如果您不介意使用正则表达式,那么此函数将为您提供重命名文件的强大功能:
因此在您的示例中,您可以这样做(假设它是文件所在的当前目录):
但您也可以回滚到初始文件名:
等等。
If you don't mind using regular expressions, then this function would give you much power in renaming files:
So in your example, you could do (assuming it's the current directory where the files are):
but you could also roll back to the initial filenames:
and more.
我用这个简单地重命名文件夹子文件夹中的所有文件,
我将用 new_str 替换所有出现的 old_str 。
I have this to simply rename all files in subfolders of folder
I am replacing all occurences of old_str with any case by new_str.
尝试: http://www.mattweber.org/2007/03/ 04/python-script-renamepy/
该程序的源代码也可用。
Try: http://www.mattweber.org/2007/03/04/python-script-renamepy/
The program's source code is also available.
我自己写了一个python脚本。 它将文件所在目录的路径以及要使用的命名模式作为参数。 但是,它会通过将增量数字(1、2、3 等)附加到您提供的命名模式来进行重命名。
希望这对你有用。
I've written a python script on my own. It takes as arguments the path of the directory in which the files are present and the naming pattern that you want to use. However, it renames by attaching an incremental number (1, 2, 3 and so on) to the naming pattern you give.
Hope this works for you.
我遇到了类似的问题,但我想将文本附加到目录中所有文件的文件名开头并使用类似的方法。 请参阅下面的示例:
I had a similar problem, but I wanted to append text to the beginning of the file name of all files in a directory and used a similar method. See example below:
对于我来说,在我的目录中我有多个子目录,每个子目录有很多图像我想将所有子目录图像更改为 1.jpg ~ n.jpg
(我自己的答案)https://stackoverflow.com/a/45734381/6329006
as to me in my directory I have multiple subdir, each subdir has lots of images I want to change all the subdir images to 1.jpg ~ n.jpg
(mys own answer)https://stackoverflow.com/a/45734381/6329006