Re: [Tutor] How to skip a single file when using shutil.make_archive()
Anthony Papillion wrote: > I'm creating an archive of a directory using shutil.make_archive and need > to skip a single file if it is present in that directory. Is there a way > to do this or should I be looking to ZipFile to meet this need? I should not post this, especially on a tutor list, but as you already have a fairly robust solution here's an evil hack: $ cat make_partial_archive.py #!/usr/bin/env python3 import os import shutil from unittest.mock import patch _os_path_isfile = os.path.isfile def accept(path): if path in ["delta/one.txt", "beta.txt"]: print("skipping %r" % path) return False return _os_path_isfile(path) if __name__ == "__main__": with patch("os.path.isfile", side_effect=accept): shutil.make_archive("archive", "zip", "data") $ tree data data ├── alpha.txt ├── beta.txt ├── delta │ ├── one.txt │ ├── three.txt │ └── two.txt └── gamma.txt 1 directory, 6 files $ python3 make_partial_archive.py skipping 'beta.txt' skipping 'delta/one.txt' $ unzip archive.zip -d tmp Archive: archive.zip inflating: tmp/gamma.txt inflating: tmp/alpha.txt inflating: tmp/delta/two.txt inflating: tmp/delta/three.txt $ Explanation: The script manipulates the make_archive() implementation for zip files by temporarily replacing os.path.isfile() with a custom accept() function. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to skip a single file when using shutil.make_archive()
Many thanks Ben! That is exactly what I was looking for and it's super easy. Thanks again! On Fri, Aug 14, 2015 at 5:36 PM, Ben Finney wrote: > Anthony Papillion writes: > > > I'm creating an archive of a directory using shutil.make_archive and > > need to skip a single file if it is present in that directory. Is > > there a way to do this or should I be looking to ZipFile to meet this > > need? > > You can create a hierarchy of files the way you want it, and then use > ‘shutil.make_archive’ once the tree is the way you want it. > > * Use ‘tempfile.mkdtemp’ to create a unique temporary working directory, > and bind its name to ‘working_dir’. > > * Use ‘shutil.copytree’ to copy the entire hierarchy from its permanent > location to the temporary ‘working_dir’ location. > > * Use other ‘shutil’ functions to manipulate the files in ‘working_dir’ > the way you want. > > * Use ‘shutil.make_archive’ to create an archive of the files from > ‘working_dir’. > > * Use ‘shutil.rmtree’ to remove the ‘working_dir’. > > -- > \ “All television is educational television. The question is: | > `\ what is it teaching?” —Nicholas Johnson | > _o__) | > Ben Finney > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to skip a single file when using shutil.make_archive()
Anthony Papillion writes: > I'm creating an archive of a directory using shutil.make_archive and > need to skip a single file if it is present in that directory. Is > there a way to do this or should I be looking to ZipFile to meet this > need? You can create a hierarchy of files the way you want it, and then use ‘shutil.make_archive’ once the tree is the way you want it. * Use ‘tempfile.mkdtemp’ to create a unique temporary working directory, and bind its name to ‘working_dir’. * Use ‘shutil.copytree’ to copy the entire hierarchy from its permanent location to the temporary ‘working_dir’ location. * Use other ‘shutil’ functions to manipulate the files in ‘working_dir’ the way you want. * Use ‘shutil.make_archive’ to create an archive of the files from ‘working_dir’. * Use ‘shutil.rmtree’ to remove the ‘working_dir’. -- \ “All television is educational television. The question is: | `\ what is it teaching?” —Nicholas Johnson | _o__) | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] How to skip a single file when using shutil.make_archive()
Hello Everyone, I'm creating an archive of a directory using shutil.make_archive and need to skip a single file if it is present in that directory. Is there a way to do this or should I be looking to ZipFile to meet this need? Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor