|
| 1 | +#this is a module which renames multiple files simultaneously. |
| 2 | +#It renames the given files with the provided New name or with Giving Numbers |
| 3 | +#It can even change the extension |
| 4 | + |
| 5 | +# the syntax is rename(src,new_name,numbering,extension) |
| 6 | +# if Dont want to specify new_name it can be given null (new_name='') |
| 7 | +# if want to do Numbering then (numbering=1) else by default(numbering=0) |
| 8 | +# if want to give extension then (extension='.jpg') else default is (extension='1') in which the extension dont change |
| 9 | +# if want to give no extension then (extension='0') Make Sure it is a string |
| 10 | + |
| 11 | + |
| 12 | +import os |
| 13 | +def rename(path,new_name='',numbering=0,extension='1'): #default arguments |
| 14 | + # print(numbering) |
| 15 | + # print(extension) |
| 16 | + # print(new_name) |
| 17 | + list=os.listdir(path) #lists all the files in the path |
| 18 | + os.chdir(path) |
| 19 | +#These below are the diffrent conditions based on which we rename the files |
| 20 | + try: |
| 21 | + if(new_name!='' and numbering==0 and extension=='0'): |
| 22 | + |
| 23 | + for i in list: #Taking each item of the folder |
| 24 | + |
| 25 | + f_name,f_ext=os.path.splitext(i) #spliting of name and extension |
| 26 | + os.rename(i,new_name) #renaming the file |
| 27 | + |
| 28 | + if(new_name!='' and numbering!=0 and extension=='0'): |
| 29 | + count=numbering |
| 30 | + for i in list: |
| 31 | + |
| 32 | + os.rename(i,new_name+str(count)) |
| 33 | + count+=1 |
| 34 | + if(new_name!='' and numbering!=0 and extension=='1'): |
| 35 | + count=numbering |
| 36 | + for i in list: |
| 37 | + |
| 38 | + f_name,f_ext=os.path.splitext(i) |
| 39 | + |
| 40 | + os.rename(i,new_name+str(count)+f_ext) |
| 41 | + count+=1 |
| 42 | + if(new_name!='' and numbering==0 and extension=='1'): |
| 43 | + |
| 44 | + for i in list: |
| 45 | + f_name,f_ext=os.path.splitext(i) |
| 46 | + os.rename(i,new_name+f_ext) |
| 47 | + if(new_name!='' and numbering!=0 and (extension!='1' and extension!='0')): |
| 48 | + count=numbering |
| 49 | + for i in list: |
| 50 | + |
| 51 | + f_name,f_ext=os.path.splitext(i) |
| 52 | + os.rename(i,new_name+str(count)+extension) |
| 53 | + count+=1 |
| 54 | + if(new_name!='' and numbering==0 and (extension!='1' and extension!='0')): |
| 55 | + |
| 56 | + for i in list: |
| 57 | + f_name,f_ext=os.path.splitext(i) |
| 58 | + os.rename(i,new_name+extension) |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + if(new_name=='' and numbering!=0 and extension=='0'): |
| 63 | + count=numbering |
| 64 | + for i in list: |
| 65 | + |
| 66 | + f_name,f_ext=os.path.splitext(i) |
| 67 | + os.rename(i,f_name+str(count)) |
| 68 | + count+=1 |
| 69 | + if(new_name=='' and numbering!=0 and extension=='1'): |
| 70 | + count=numbering |
| 71 | + for i in list: |
| 72 | + |
| 73 | + f_name,f_ext=os.path.splitext(i) |
| 74 | + os.rename(i,f_name+str(count)+f_ext) |
| 75 | + count+=1 |
| 76 | + if(new_name=='' and numbering==0 and extension=='1'): |
| 77 | + |
| 78 | + for i in list: |
| 79 | + f_name,f_ext=os.path.splitext(i) |
| 80 | + os.rename(i,f_name+f_ext) |
| 81 | + if(new_name=='' and numbering==0 and extension==0): |
| 82 | + |
| 83 | + for i in list: |
| 84 | + |
| 85 | + f_name,f_ext=os.path.splitext(i) |
| 86 | + os.rename(i,i) |
| 87 | + if(new_name=='' and numbering!=0 and (extension!='1' and extension!='0')): |
| 88 | + count=numbering |
| 89 | + for i in list: |
| 90 | + |
| 91 | + f_name,f_ext=os.path.splitext(i) |
| 92 | + os.rename(i,f_name+str(count)+extension) |
| 93 | + count+=1 |
| 94 | + if(new_name=='' and numbering==0 and (extension!='1' and extension!='0')): |
| 95 | + |
| 96 | + for i in list: |
| 97 | + |
| 98 | + f_name,f_ext=os.path.splitext(i) |
| 99 | + os.rename(i,f_name+extension) |
| 100 | + except WindowsError: |
| 101 | + print("Something Went Wrong, Renaming is not Finished.") |
| 102 | + |
| 103 | +#path='C:\Users\Harsh\PycharmProjects\color_detect-using-ML\data\input_data\NOT_RED' |
| 104 | +#rename(path,'not_red',1,) |
0 commit comments