Python Forum
Rename docx file from tuple - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Rename docx file from tuple (/thread-30427.html)



Rename docx file from tuple - gjack - Oct-20-2020

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

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



RE: Rename docx file from tuple - bowlofred - Oct-20-2020

Is the eft and file_name data going to come from different sources? If so, assembling them is fine. But if they were just in my code, I'd keep them together (as a tuple) rather than as two different lists.

You read the filename into file, and then assign it to src, never using file again. If that's your intention, use src as the for loop variable and get rid of file.

Your main problem is that line 16 is outside the block above that looks at all the files. So lines 11-16 run for all the files, and then the block exits. Line 16 is only run one time, and src is set to whatever the last file found in the for loop was. If you want to run that block on all the files, it needs to be indented within the block above.


RE: Rename docx file from tuple - gjack - Oct-20-2020

(Oct-20-2020, 05:08 PM)bowlofred Wrote: Is the eft and file_name data going to come from different sources? If so, assembling them is fine. But if they were just in my code, I'd keep them together (as a tuple) rather than as two different lists.

You read the filename into file, and then assign it to src, never using file again. If that's your intention, use src as the for loop variable and get rid of file.

Your main problem is that line 16 is outside the block above that looks at all the files. So lines 11-16 run for all the files, and then the block exits. Line 16 is only run one time, and src is set to whatever the last file found in the for loop was. If you want to run that block on all the files, it needs to be indented within the block above.
Thank you for the assistance. I now see what you are saying about having a single tuple, over the 2 lists I created. That I will change. I also understand your comment on my use of file and src, and will also change that. After I indented my IF statement over, 1 of my files renamed successfully. I need to make a few other changes and may have more questions, but this has helped me move forward. Thanks!