Oct-20-2020, 04:41 PM
I am fairly new to Python, so if my methods can be improved, please advise.
I am using Python 3.8
I have created a tuple from 2 lists using zip. My intention is to take this tuple and compare to a list of files names that will have the the same values as index[0] of my tuple. There are 100 + files and I am not wanting to rename these manually. In my tuple index[1] contains the new file name. If index[0] is found in a file name, then rename to index[1].
In my code when I check to see if my tuple is == to a file name, no data returns. So I know that is an issue. My print statement is just for testing. What else do I need to do, to see if my EFTxxxxxxx tuple matches up to a file name, so I can rename the file to the name in my tuple?
I have 4 files I am testing to rename before doing all 100.
EFT0002078.docx
EFT0002079.docx
EFT0002080.docx
EFT0002081.docx
I am using Python 3.8
I have created a tuple from 2 lists using zip. My intention is to take this tuple and compare to a list of files names that will have the the same values as index[0] of my tuple. There are 100 + files and I am not wanting to rename these manually. In my tuple index[1] contains the new file name. If index[0] is found in a file name, then rename to index[1].
In my code when I check to see if my tuple is == to a file name, no data returns. So I know that is an issue. My print statement is just for testing. What else do I need to do, to see if my EFTxxxxxxx tuple matches up to a file name, so I can rename the file to the name in my tuple?
I have 4 files I am testing to rename before doing all 100.
EFT0002078.docx
EFT0002079.docx
EFT0002080.docx
EFT0002081.docx
import os eft = ['EFT0002078','EFT0002079','EFT0002080','EFT0002081'] file_name = ['200061564_202010_1ST ALLERGY ASTHMA', '200088994_202010_3G MEDICAL', '200060694_202010_ACADEMY PARK', '200073620_202010_ASSOCIATES, PC'] os.chdir('C:\\Users\\glenn.jack\\Python3.8') tuplex = list(zip(eft, file_name)) tupley = tuplex[0] i=1 for file in os.listdir(): src = file dsta = tupley[0] dst = tupley[1] print (dsta + ', ' + src + ', ' + dst) if dsta in src: #print (dst) os.rename(src,dst + '.docx') i+=1
Output:EFT0002078, EFT0002078.docx, 200061564_202010_1ST ALLERGY ASTHMA
EFT0002078, EFT0002079.docx, 200061564_202010_1ST ALLERGY ASTHMA
EFT0002078, EFT0002080.docx, 200061564_202010_1ST ALLERGY ASTHMA
EFT0002078, EFT0002081.docx, 200061564_202010_1ST ALLERGY ASTHMA