Discussion:
[Tutor] How to skip a single file when using shutil.make_archive()
Anthony Papillion
2015-08-14 22:27:04 UTC
Permalink
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 - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Ben Finney
2015-08-14 22:36:05 UTC
Permalink
Post by Anthony Papillion
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 - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mail
Anthony Papillion
2015-08-14 22:57:47 UTC
Permalink
Many thanks Ben! That is exactly what I was looking for and it's super
easy. Thanks again!
Post by Ben Finney
Post by Anthony Papillion
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
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.pyt
Peter Otten
2015-08-15 06:20:25 UTC
Permalink
Post by Anthony Papillion
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 - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/m

Loading...