'python zipfile multiple files

i am having problems with python and zipfile, namely: I can't add a second file to my zip. here is my code, if you need more, I'll be glad to provide it.

def zipDir(fn_source, fn_destinationFolder):
  ''' fn_destinationFolder = folder to zip
      fn_source = destination path for the zip
  '''
  fn_zipfileName = os.path.join(os.path.dirname(os.path.basename(fn_source)),fn_destinationFolder)+'.zip'

  with zipfile.ZipFile(fn_zipfileName, 'w') as fn_zipfile:
    for csvFile in os.listdir(fn_destinationFolder):
#     fn_zipfile.write(csvFile)
      if csvFile.endswith('.csv'):
        try:
          # fn_zipfile.write(os.path.join(fn_destinationFolder,csvFile))   ## write the whole filestructure to zip, '/home/uname/.../xyz.zip'
          fn_zipfile.write(csvFile)                                       ## write only the file to zip  'xyz.zip'
        except IOError:
          None
          print('did not add',csvFile,'to',fn_zipfile)
    fn_zipfile.printdir()
  return fn_zipfileName

in the "test" dir, I have 2 files: "example.csv" and "example1.csv" this results in the following error:

python3.5 removing_the_header_from_CSV.py --from test --to zip
Removed headers from example.csv
Removed headers from example1.csv
did not add example1.csv to <zipfile.ZipFile filename='/home/xxx/repos/automate_the_boring_stuff/14_csv_files_and_json_data/NEW_test_2016-05-22_12-19-23.zip' mode='w'>
File Name                                             Modified             Size
example.csv                                    2016-05-20 17:12:19          191
zipfile can be found at /home/xxx/repos/automate_the_boring_stuff/14_csv_files_and_json_data/NEW_test_2016-05-22_12-19-23.zip

if I only use the "fn_zipfile.write(csvFile)", I get this extra info:

python3.5 removing_the_header_from_CSV.py --from test --to zip
Removed headers from example.csv
Removed headers from example1.csv
Traceback (most recent call last):
  File "removing_the_header_from_CSV.py", line 169, in <module>
main()
  File "removing_the_header_from_CSV.py", line 156, in main
zipfileName = zipDir(source, destinationFolder)
  File "removing_the_header_from_CSV.py", line 112, in zipDir
fn_zipfile.write(csvFile)
  File "/usr/lib/python3.5/zipfile.py", line 1433, in write
st = os.stat(filename)
FileNotFoundError: [Errno 2] No such file or directory: 'example1.csv'

I tried this with different files and always the same result except when I only have 1 file in my test dir. Then, all works as expected.



Solution 1:[1]

One of your problems is that you are listing the contents of some remote source directory, for some reason called fn_destinationFolder, to get the file names, filtering for .csv - lower case only and then telling the zipfile to add the filename. This is like saying to someone, who is in London, get a yellow cab at the junction of 1st and Main after checking that those streets exist in New York and that there is a cab rank there.

Make sure that you pass fn_zipfile.write the full path to the input file - you can also override the name inside of the zip file, e.g.:

fn_zipfile.write(os.path.join(fn_destinationFolder, csvFile), csvFile)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steve Barnes