Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rename docx file from tuple
#1
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
Reply
#2
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.
Reply
#3
Thumbs Up 
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  no module named 'docx' when importing docx MaartenRo 1 889 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  Rename first row in a CSV file James_S 3 589 Dec-17-2023, 05:20 AM
Last Post: James_S
  Replace a text/word in docx file using Python Devan 4 3,458 Oct-17-2023, 06:03 PM
Last Post: Devan
  rename file RolanRoll 0 535 May-18-2023, 02:17 PM
Last Post: RolanRoll
  '' FTP '' File upload with a specified string and rename midomarc 1 1,170 Apr-17-2023, 03:04 AM
Last Post: bowlofred
  rename same file names in different directories elnk 0 718 Nov-04-2022, 05:23 PM
Last Post: elnk
  Use module docx to get text from a file with a table Pedroski55 8 6,196 Aug-30-2022, 10:52 PM
Last Post: Pedroski55
  python-docx regex: replace any word in docx text Tmagpy 4 2,246 Jun-18-2022, 09:12 AM
Last Post: Tmagpy
  rename and add desire "_date" to end of file name before extention RolanRoll 1 1,248 Jun-13-2022, 11:16 AM
Last Post: gruntfutuk
  Rename Files based on XML file klturi421 3 2,205 Oct-22-2021, 07:37 PM
Last Post: klturi421

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020