The examples in this article describe how Python implements bulk modification of file names. Share to everyone for your reference. Specific as follows:
Download the storytelling "He he legend", the file name contains xxx audio download, with a script to remove it. Scripts involve Os.rename renaming methods, Str.partition method use, and regular Match,search method differences
# encoding:utf-8### file name such as: # Helong legend \d+[www.ysx8.com].mp3# #import os,refs=os.listdir (' Shan Tianfang _ The legend ') for F in fs: ##### #方法一: Partition gets The #1 of the useless characters. The file name is divided into 3 parts #ls =f.partition (' [') #2. Ls[0] to require a file name, so get ls[1:] # dirtystring = ". Join (ls[1:]) #3. Start replacing #newname =f.replace (dirtystring, ') + '. mp3 ') #os. Rename (' Shan Tianfang _ Long legend/' + F, newname) ##### #方法二: Regular gets the useless characters Dirtymatch = Re.search (R ' \[.*?\] ', f) if Dirtymatch: Dirtystring=dirtymatch.group (0) newname=f.replace (dirtystring, ') + '. mp3 ' os.rename (' Shan Tianfang _ Helong Legend/' + F, newname) #注意: can be directly replaced with the Re.sub method does not require characters in the file name
Hopefully this article will help you with Python programming.