我想通过(Python)为解压缩(.tar.gz)文件创建一个脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30887979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I want to create a script for unzip (.tar.gz) file via (Python)
提问by Alex
I am trying to make a script for unzipping all the .tar.gz files from folders in one directory. For example, I will have a file which it calls ( testing.tar.gz). Then if I do manually, I can press to "extract here" then the .tar.gz file will create a new file, and it calls testing.tar. Finally, if I repeat the process of pressing "extract here", the .tar file prodcudes me all the .pdf files.
我正在尝试制作一个脚本,用于从一个目录中的文件夹中解压缩所有 .tar.gz 文件。例如,我将有一个它调用的文件(testing.tar.gz)。然后,如果我手动执行,我可以按“在此处提取”,然后 .tar.gz 文件将创建一个新文件,并调用 testing.tar。最后,如果我重复按“在此处提取”的过程,.tar 文件会生成所有 .pdf 文件。
I wonder that how can I do it, and I have my code here and it seems doesn't realty work tho.
我想知道我该怎么做,我在这里有我的代码,但它似乎不起作用。
import os
import tarfile
import zipfile
def extract_file(path, to_directory='.'):
if path.endswith('.zip'):
opener, mode = zipfile.ZipFile, 'r'
elif path.endswith('.tar.gz') or path.endswith('.tgz'):
opener, mode = tarfile.open, 'r:gz'
elif path.endswith('.tar.bz2') or path.endswith('.tbz'):
opener, mode = tarfile.open, 'r:bz2'
else:
raise ValueError, "Could not extract `%s` as no appropriate extractor is found" % path
cwd = os.getcwd()
os.chdir(to_directory)
try:
file = opener(path, mode)
try: file.extractall()
finally: file.close()
finally:
os.chdir(cwd)
回答by Lye Heng Foo
Why do you want to "press" twice to extract a .tar.gz, when you can easily do it once? Here is a simple code to extract both .tar and .tar.gz in one go:
为什么要“按”两次以提取 .tar.gz,而您可以轻松完成一次?这是一个简单的代码,可以一次性提取 .tar 和 .tar.gz:
import tarfile
if fname.endswith("tar.gz"):
tar = tarfile.open(fname, "r:gz")
tar.extractall()
tar.close()
elif fname.endswith("tar"):
tar = tarfile.open(fname, "r:")
tar.extractall()
tar.close()
回答by Beckett O'Brien
When I ran your program, it worked perfectly for a tar.gz and a .tgz file, it didn't give me the correct items when I opened the zip, but .tbz was the only one that raised an error. I think you used the wrong method to unpack a .tbz because the error said I had an incorrect file type, but I didn't. One way you could solve the .zip issue is to us os.command() and unzip it with a command line (depending on your os) because it returned a _MACOSX folder with nothing inside of it even though I entered the path correctly. The only other error I encountered was that you used improper syntax for raising an error.
This is what you should have used:
当我运行你的程序时,它非常适合 tar.gz 和 .tgz 文件,当我打开 zip 时它没有给我正确的项目,但 .tbz 是唯一引发错误的项目。我认为您使用了错误的方法来解压 .tbz,因为错误说我的文件类型不正确,但我没有。您可以解决 .zip 问题的一种方法是对我们 os.command() 并使用命令行(取决于您的操作系统)解压缩它,因为它返回了一个 _MACOSX 文件夹,其中没有任何内容,即使我正确输入了路径。我遇到的唯一其他错误是您使用了不正确的语法来引发错误。
这是你应该使用的:
raise ValueError("Error message here")
You used a comma and no parenthesis. Hope this helps!
您使用了逗号且没有括号。希望这可以帮助!
回答by Ehsan
You can execute a shell script from Python using envoy:
您可以使用 envoy 从 Python 执行 shell 脚本:
import envoy # pip install envoy
if (file.endswith("tar.gz")):
envoy.run("tar xzf %s -C %s" % (file, to_directory))
elif (file.endswith("tar")):
envoy.run("tar xf %s -C %s" % (file, to_directory))
回答by mickours
If you are using python 3, you should use shutil.unpack_archivethat works for most of the common archive format.
如果您使用的是 python 3,则应该使用适用于大多数常见存档格式的shutil.unpack_archive。
shutil.unpack_archive(filename[, extract_dir[, format]])
Unpack an archive. filename is the full path of the archive. extract_dir is the name of the target directory where the archive is unpacked. If not provided, the current working directory is used.
Shutil.unpack_archive(文件名[,extract_dir[,格式]])
解压存档。filename 是存档的完整路径。extract_dir 是解压缩存档的目标目录的名称。如果未提供,则使用当前工作目录。
For example:
例如:
def extract_all(archives, extract_path):
for filename in archives:
shutil.unpack_archive(filename, extract_path)
回答by Taras Vaskiv
Using context manager:
使用上下文管理器:
import tarfile
<another code>
with tarfile.open(os.path.join(os.environ['BACKUP_DIR'],
f'Backup_{self.batch_id}.tar.gz'), "r:gz") as so:
so.extractall(path=os.environ['BACKUP_DIR'])