Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Failing to Zip files
#1
Hi, I'm trying to find files that have digits in a file name.
then I'd like to zip files to another directory.
My code finds the files I want and moves them to a directory I want but not as ZIP files Sad .
Any help will be appreciated.

from zipfile import ZipFile
import os
import glob
import os.path
 
pth = 'c:\\02'
zout= 'C:\\0001\\'
for fname in glob.glob('C:\\02\\*[0-9].*'):
    basename = os.path.basename(fname)
    fz = zout+basename
    with ZipFile(fz,'w') as zip: 
        zip.write(fz)
Reply
#2
Do you want to write them all into one zipfile or each one into separate zipfiles?

You set pth, but then never use it, with c:\\02 hardcoded.

Line 11 is where you name the zipfile.
Line 12 is where you name the file you want to put in the zipfile.

You've used fz for both of these, which is incorrect. Probably you want to write the fname to the zip file.
tester_V likes this post
Reply
#3
Thanks bowlofred!

I tried your suggestions.
Still the script only copies files not zipps then up.
I actually tried to open one of the copied files, they all have extensions '.TXT' but it is not a'.TXT' file. I cannot really open it up. SO the script does something to the files.
here is a new version of the script:
from zipfile import ZipFile
import os
import glob
import os.path

zout= 'C:\\0001\\'
for fname in glob.glob('C:\\02\\*[0-9].*'):
    print (fname)
    basename = os.path.basename(fname)
    fz = zout+basename
    print (fz)
    with ZipFile(fz,'w') as zip: 
        zip.write(fname)
Reply
#4
How are you trying to open it? It's a zipfile, but you didn't change the name, so it's got the same name as it had before. You should name the zipfile with a .zip ending.

for fname in glob.glob('C:\\02\\*[0-9].*'):
    print (fname)
    basename = os.path.basename(fname)
    fz = zout+basename
    print (fz)
    zipname = os.path.splitext(fz)[0] + ".zip"  # Change name to have .zip extension
    with ZipFile(zipname,'w') as zip:           # Use .zip name for zipfile
        zip.write(fname)
tester_V likes this post
Reply
#5
to bowlofred:
Thanks man!
I actually come up with my own solution.
It is not as short as yours... Smile

zout= 'C:\\0001\\'
for fname in glob.glob('C:\\02\\*[0-9].*'):
    basename = os.path.basename(fname)
    just_n = os.path.splitext(basename)[0]   
  
    fz = zout+just_n+'.zip'

    with ZipFile(fz,'w') as zip: 
        zip.write(fname,arcname=basename)
        print (zip)
I really appreciate your help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Failing regex, space before and after the "match" tester_V 6 1,115 Mar-06-2023, 03:03 PM
Last Post: deanhystad
  Failing to print sorted files tester_V 4 1,188 Nov-12-2022, 06:49 PM
Last Post: tester_V
  Failing reading a file and cannot exit it... tester_V 8 1,754 Aug-19-2022, 10:27 PM
Last Post: tester_V
  Failing regex tester_V 3 1,143 Aug-16-2022, 03:53 PM
Last Post: deanhystad
  Failing to connect to a host with WMI tester_V 6 4,279 Aug-10-2021, 06:25 PM
Last Post: tester_V
  Failing to get Stat for a Directory tester_V 11 3,475 Jul-20-2021, 10:59 PM
Last Post: Larz60+
  Windows 10 Installation failing (0x80070005) AdaptedLogic 4 6,060 Dec-31-2020, 07:39 AM
Last Post: caleb_cruze
  Python 3.8 on mac failing to start sgandon 0 2,842 Jan-14-2020, 10:58 AM
Last Post: sgandon
  trying to install oandapy and failing ErnestTBass 0 1,874 Feb-24-2019, 06:13 PM
Last Post: ErnestTBass
  Pyinstaller failing JP_ROMANO 2 4,020 Jan-16-2019, 06:07 PM
Last Post: JP_ROMANO

Forum Jump:

User Panel Messages

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