简体   繁体   中英

Python or linux command line detect directory name in .tar.gz

I'm automating a process to upload files and work with them in AWS. I have a python script and I upload somefolder.tar.gz . Typically these files just extract a folder named somefolder but sometimes it is different folder name. Is there anyway to detect what the folder will be named on extraction?

Currently I am performing the extraction through the boto protocol:

output = ssh_client.run('cd ~/Sharing && tar -zxvf ' + tar_filename)

tar_filename is something like: filename.tar.gz One solution could be to just chop the .tar.gz and make sure all the extracted folders are always named the same as the .tar.gz

tar -tf will list all the files in the tar, without extracting.

You can then parse the output to find what the top-level directory is.

Can you examine the file contents before you upload file to AWS? If you can, you just list the contents with tzf parameters to make sure the naming is correct, or you can do the same thing with built-in python tarfile .

您可以使用命令tar --list -f your_file.tar.gz列出tar.gz的内容。

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