Python File _ Batch renaming

Source: Internet
Author: User

1 #!/usr/bin/env python2 #CODING=GBK3 #file Operation Instance--add ' _test ' to all picture names under the folder4 ImportRe,os,time5 #str.split (path) split string6 #' connectors '. Join (list) to make a list of strings7 8 defchange_name (path):9     GlobalI#This is defined in the function, the definition will be error outside.Ten #determines whether the path is a file or directory, and if not, returns an error One     if  notOs.path.isdir (PATH) and  notos.path.isfile (path): A         returnfalse -     ifos.path.isfile (path): -File_path=os.path.split (PATH)#decomposition file path composition list deposit File_path theLists=file_path[1].split ('.')#split files with file name extensions -FILE_EXT=LISTS[-1]#remove suffix name equivalent to: file_ext=lists[1],list only two numbers -img_ext=['BMG','JPEG','gif','PSD','PNG','jpg']#picture suffix name -         ifFile_extinchImg_ext: +             #This will be the new file path, the name of the word will be list[0] replaced -Os.rename (path,file_path[0]+'/'+lists[0]+'_test.'+file_ext) +I=i+1 A     elifOs.path.isdir (path): at          forXinchOs.listdir (path): -             #to add a file to a directory after a file by using join -Change_name (Os.path.join (path,x))#Os.path.join () is useful for path processing -Os.chdir ("g:/file Operations") -Img_dir ='g:\\ file operations \ \ picture 1' -Img_dir = Img_dir.replace ('\\','/') inI=0 -Start=time.time ()#Start Time to change_name (Img_dir) +C=time.time ()-start#Calculate consumption Time - Print('Total%s picture processed'%(i)) the Print('program run time:%0.2f'% (c))

If the global I is defined outside the function, it will be run with an error: local variable ' i ' referenced before assignment. This error means that the variable i is not defined before the reference, but at the beginning of the definition, the Global keyword is used to solve the problem. In Python's function and the global name of the variable, if you have to modify the value of the variable will become a local variable, before the modification of the variable reference to the natural will appear undefined such an error, if you are determined to reference the global variable, and to modify it, you must add the Global keyword.

Python File _ Batch renaming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.