Adding file to existing zipfile

前端 未结 2 1729
有刺的猬
有刺的猬 2021-02-19 02:18

I\'m using python\'s zipfile module.
Having a zip file located in a path of:
/home/user/a/b/c/test.zip
And having another file created unde

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-19 03:07

    import zipfile
    
    # Open a zip file at the given filepath. If it doesn't exist, create one.
    # If the directory does not exist, it fails with FileNotFoundError
    filepath = '/home/user/a/b/c/test.zip'
    with zipfile.ZipFile(filepath, 'a') as zipf:
        # Add a file located at the source_path to the destination within the zip
        # file. It will overwrite existing files if the names collide, but it
        # will give a warning
        source_path = '/home/user/a/b/c/1.txt'
        destination = 'foobar.txt'
        zipf.write(source_path, destination)
    

提交回复
热议问题