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 to connect by 'net use' tester_V 1 341 Apr-20-2024, 06:31 AM
Last Post: tester_V
  Failing to print sorted files tester_V 4 1,422 Nov-12-2022, 06:49 PM
Last Post: tester_V
  Failing regex tester_V 3 1,324 Aug-16-2022, 03:53 PM
Last Post: deanhystad
  Failing to connect to a host with WMI tester_V 6 4,624 Aug-10-2021, 06:25 PM
Last Post: tester_V
  Failing to get Stat for a Directory tester_V 11 3,780 Jul-20-2021, 10:59 PM
Last Post: Larz60+
  Python 3.8 on mac failing to start sgandon 0 3,113 Jan-14-2020, 10:58 AM
Last Post: sgandon
  trying to install oandapy and failing ErnestTBass 0 2,016 Feb-24-2019, 06:13 PM
Last Post: ErnestTBass
  Pyinstaller failing JP_ROMANO 2 4,196 Jan-16-2019, 06:07 PM
Last Post: JP_ROMANO
  Why is my for loop failing? microphone_head 4 3,095 Sep-11-2018, 01:21 PM
Last Post: microphone_head

Forum Jump:

User Panel Messages

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