Python Forum
tar module - how usable is it?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tar module - how usable is it?
#4
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!
Reply


Messages In This Thread
tar module - how usable is it? - by Skaperen - Nov-28-2018, 07:46 PM
RE: tar module - how usable is it? - by Gribouillis - Nov-28-2018, 08:21 PM
RE: tar module - how usable is it? - by Skaperen - Nov-29-2018, 01:50 AM
RE: tar module - how usable is it? - by DeaD_EyE - Nov-29-2018, 10:45 AM
RE: tar module - how usable is it? - by Skaperen - Dec-07-2018, 03:44 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020