使用python重命名文件 [英] file renaming using python
问题描述
我正在尝试重命名一个文件,该文件是由某些本地模块自动生成的,但是我想知道是否使用os.listdir
是我过滤/缩小此文件的唯一方法.
该文件将始终在删除之前生成,并且代码将根据列表中的下一项生成下一个文件(仍在同一目录中).
I am trying to rename a file in which it is auto-generated by some local modules but I was wondering if using os.listdir
is the only way for me to filter/ narrow down this file.
This file will always be generated before it is removed and the code will generate the next one (still in the same directory) based on the next item in list.
基本上,每当生成此文件时,它都会位于以下文件路径中:
/user_data/.tmp/tempLibFiles/2015_03_16_192212_182096.con
Basically, whenever this file is generated, it comes in the following file path:
/user_data/.tmp/tempLibFiles/2015_03_16_192212_182096.con
我只想将2015_03_16_192212_182096
重命名为connectionFile
,而其余部分保持不变.
I had only wanted to rename the 2015_03_16_192212_182096
into connectionFile
while keeping the rest the same.
推荐答案
您还可以使用glob
模块将文件列表缩小到与特定模式匹配的文件列表.例如:
You can also use the glob
module to narrow down the list of files to the one that matches a particular pattern. For example:
import glob
files = glob.glob('/user_data/.tmp/tempLibFiles/*.con')
这篇关于使用python重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!