ambikanair
ambikanair

Reputation: 4560

Tar function in Python

I am trying to tar a folder using the following code.

    make_tarfile('logs_' + str(datetime.datetime.now()),logFolder)


def make_tarfile(output_filename, source_dir):
    with closing(tarfile.open(output_filename, "w:gz")) as tar:
        tar.add(source_dir, arcname=os.path.basename(source_dir))

I dont see any tar file created though.Please could any one correct my code.

Thanks in advance

Upvotes: 0

Views: 255

Answers (1)

Jan Vlcinsky
Jan Vlcinsky

Reputation: 44092

  • your code looks usable, you shall find the archive created.
  • be aware, the archive file name will be exactly the name, you pass into tarfile.open, you have to specify the extension .tar.gz if you want to see it as name of the archive.
  • the with closing is not necessary in Python 2.7+, you may use with tarfile.open(output_filename, "w:gz")) as tar: as open tarfile has proper context manager available.

Upvotes: 1

Related Questions