简体   繁体   中英

Python created tar.gz file contains “_” folder, how to remove?

The.tar.gz files I'm created via Python contain a "_" root-level folder which I need to remove.

Here's the.tar.gz function I'm using:

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

I create the.tar.gz with: make_tarfile('ARCHIVE.tar.gz', 'C:\FolderA')

As you can see, there is a "_" folder added to the.tar.gz. Any suggestions on how I remove it? Interestingly enough, when I extract the.tar.gz, the " _ " folder doesn't appear. In that sense, it's fine. But this would be a.tar.gz consumed by many users, so I'd like to have it not contain quirks like this.

What you see in the 7z interface is the .. entry that it adds to every directory so that you can navigate up by clicking on it. It does not exist in the tar archive.

Consider the following:

def make_tarfile(output_filename, source_dir):
    with tarfile.open(output_filename, "w:gz") as tar:
        for name in os.listdir(source_dir):
            tar.add(os.path.join(source_dir, name), arcname=name)

When called as make_tarfile('ARCHIVE.tar.gz', 'FolderA') where FolderA is:

FolderA/
|-- FolderB
|   `-- example
`-- example

Results in an archive containing:

example
FolderB/
FolderB/example

Now I don't like this as I prefer my tar files to extract into a top level directory. As they were created with tar -czf ARCHIVE.tar.gz FolderA/ and have the entries:

FolderA/
FolderA/example
FolderA/FolderB/
FolderA/FolderB/example

In case anyone runs into this exact scenario--use 7Zip CLI and do a wildcard copy of all the contents of the desired folder (the parent folder will be omitted). Like this:

subprocess.call(['C:\Program Files\\7-Zip\\7z.exe', 'a', '-ttar', 'C:\ARCHIVE.tar', 'C:\FolderA\*'])
subprocess.call(['C:\Program Files\\7-Zip\\7z.exe', 'a', '-tgzip', 'C:\ARCHIVE.tar.gz', 'C:\ARCHIVE.tar'])

No "_" folder will be in the archive either, it'll be nice and clean:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM