OS File Operations | Batch modify picture file names by series | Python programming

Source: Internet
Author: User

1. File Operations1.1Backup Files" "Backup Files" "        if __name__=="__main__":            #Please enter a file name to copyOld_file_name = input ('Please enter the file to be copied:')            #Open old fileOld_file = open (Old_file_name,'RB')            #write a new file name, on top of the old file, add [duplicate]            #***.txt            #find '. ' The SubscriptPoint_index = Old_file_name.rfind ('.')            #here to determine whether this is a file or folder, folder does not have a dot            ifPoint_index >0:#re-grouping new file namesNew_file_name = old_file_name[:p Oint_index] +'[Copy parts]'+Old_file_name[point_index:]#Open the new file, note that the file name is not quoted, itself is a string;New_file = open (New_file_name,'WB')            #read old file, write to new file             forContentinchold_file.readlines (): new_file.write (content)#Close Fileold_file.close () new_file.close ( )1.2Modify file name"""Batch Modify file name"""            #Batch prefix the file name beforeImportOS funflag= 1#1 means Add flag 2 for delete flagFolderName='./renamedir/'            #gets all file names for the specified pathDirlist =Os.listdir (folderName)#Traverse output all file names forNameinchdirlist:Print(name)ifFunflag = = 1: NewName='[Dong Brothers production]'+nameelifFunflag = = 2: Num= Len ('[Dong Brothers production]') NewName=Name[num:]PrintnewName os.rename (folderName+name, foldername+newName)#Enhanced Version        " "Batch Modify picture name" "123.jpg 324.jpg 432.jpg>>>>> 1.jpg 2.jpg 3.jpgImportOSif __name__=='__main__':            #Input File pathImg_path ='/home/python/desktop/python Review/images'            #get a list of all pictures under the pathImg_file_list =Os.listdir (Img_path)#get the number of picturesImg_num =Len (img_file_list)#Traverse Pictures, replace prefixesi = 1 forImginchimg_file_list:ifImg.endswith ('. jpg'):                    #stitching path and image name                    #at firstOld_img_name =os.path.join (Img_path, IMG)#after modificationNew_img_name = Os.path.join (Img_path, str (i) +'. jpg')                    #Replace file name                    Try: Os.rename (old_img_name, new_img_name) I+ = 1exceptException as Error:Print('something went wrong, go ahead!')                        Continue                    #Print the number of successful pictures                    Print('Total Pictures:%d, now the picture is the first few:%d'% (Img_num, i))
2. OS Module1. File Rename os module rename () can complete renaming of files rename (need to modify file name, new filename)2Remove () removes files from the file OS module remove (the file name to be deleted)3. Create a folder Os.mkdir ("Zhang San")    4. Get current directory os.getcwd ()5. Change the default directory Os.chdir (".. /")    6. Get directory list Os.listdir ("./")    7. Delete Folder Os.rmdir ("Zhang San")

OS File Operations | Batch modify picture file names by series | Python programming

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.