Python批量重命名文件

最近从iData中下载了很多学术论文,这些论文文件名都以“www.cn-ki.net_”开头,一个个重命名太麻烦,于是使用如下python3脚本批量重命名文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Python3 code to rename multiple  
# files in a directory or folder

# importing os module
import os

# Function to rename multiple files
def main():

# search file in current directory
for filename in os.listdir("."):

if os.path.isfile(filename):
if "www.cn-ki.net_" in filename[0:14]:
src=filename
dst=filename[14:]
# rename the special file
os.rename(src, dst)

# Driver Code
if __name__ == '__main__':

# Calling main() function
main()

参考文献

  1. How to sort with lambda in Python,by linuxhint.
  2. 用Python复制文件的9个方法,by 景略集智.
  3. shutil — 高阶文件操作,by python.
  4. python glob.glob使用,by mantoureganmian.