Python 解压tar.gz
时间: 2023-08-02 13:08:06 浏览: 48
你可以使用`tarfile`模块来解压tar.gz文件。下面是一个示例代码:
```python
import tarfile
def extract_tar_gz(file_path, output_path):
with tarfile.open(file_path, "r:gz") as tar:
tar.extractall(output_path)
# 使用示例
file_path = "example.tar.gz" # 替换为你要解压的文件路径
output_path = "output_folder" # 替换为你要输出的文件夹路径
extract_tar_gz(file_path, output_path)
```
在这个示例中,`extract_tar_gz`函数接受两个参数:`file_path`是要解压的tar.gz文件的路径,`output_path`是解压后文件的输出目录。你可以根据需要修改这两个参数。
相关问题
python 解压tar.gz 文件
你可以使用`tarfile`模块来解压`.tar.gz`文件。以下是一个示例代码:
```python
import tarfile
def extract_tar_gz(file_path, extract_path):
with tarfile.open(file_path, 'r:gz') as tar:
tar.extractall(path=extract_path)
# 使用示例
file_path = 'example.tar.gz' # 压缩文件的路径
extract_path = 'extracted_files' # 解压缩后文件的存放路径
extract_tar_gz(file_path, extract_path)
```
在上面的示例中,`extract_tar_gz`函数接收两个参数:`file_path`是压缩文件的路径,`extract_path`是解压缩后文件的存放路径。函数使用`tarfile.open()`打开压缩文件,使用`tar.extractall()`将所有文件解压缩到指定路径。
你需要将`file_path`和`extract_path`替换为你实际的文件路径和解压缩路径。
python解压tar.gz的文件
以下是Python解压tar.gz文件的示例代码:
```python
import tarfile
# 打开tar.gz文件
filename = "文件1.tar.gz"
tf = tarfile.open(filename)
# 解压文件到指定目录
tf.extractall(path="D:/Learn/目的文件夹")
```
以上代码中,首先使用`tarfile`模块的`open()`函数打开tar.gz文件,然后使用`extractall()`函数将文件解压到指定目录中。