Nov-29-2018, 10:45 AM
Why should it not be usable? By the way, doing it as generator, you can learn much about generators.
from tarfile import TarFile from pathlib import Path def list_files_gen(root, globbing): root = Path(root) for path in root.glob(globbing): yield path def tar_gen(filename): print('Generator started') with TarFile(filename, 'w') as tar: while True: try: print('Before yield') file = yield print(f'After yield. File: {file}') except GeneratorExit: print('Generator Exit') break tar.add(file, recursive=False) print('TarFile is closed') def pack_all(archive, root, globbing): tar = tar_gen(archive) tar.send(None) for file in list_files_gen(root, globbing): tar.send(file) tar.close() pack_all('foo.tar.gz', '.', '*.py')
Output:andre@andre-GP70-2PE:~$ python pack.py
Generator started
Before yield
After yield. File: viruses.py
Before yield
After yield. File: not_working.py
Before yield
After yield. File: test.py
Before yield
After yield. File: tests.py
Before yield
After yield. File: t.py
Before yield
After yield. File: pack.py
Before yield
Generator Exit
TarFile is closed
Output:andre@andre-GP70-2PE:~$ tar --list -f foo.tar.gz
viruses.py
not_working.py
test.py
tests.py
t.py
pack.py
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!