python 从文件中提取文件时出错
python giving error in extracting a file from the file
Python代码:
import tarfile,os
import sys
def extract_system_report (tar_file_path):
extract_path = ""
tar = tarfile.open(sys.argv[1])
for member in tar.getmembers():
print ("member_name is: ")
print (member.name)
tar.extract(member.name, "./crachinfo/")
extract_system_report(sys.argv[1])
提取文件时出现以下错误:
>> python tar_read.py /nobackup/deepakhe/POLARIS_POJECT_09102019/hackathon_2021/a.tar.gz
member_name is:
/bootflash/.prst_sync/reload_info
Traceback (most recent call last):
File "tar_read.py", line 38, in <module>
extract_system_report(sys.argv[1])
File "tar_read.py", line 10, in extract_system_report
tar.extract(member.name, "./crachinfo/")
File "/auto/pysw/cel8x/python64/3.6.10/lib/python3.6/tarfile.py", line 2052, in extract
numeric_owner=numeric_owner)
File "/auto/pysw/cel8x/python64/3.6.10/lib/python3.6/tarfile.py", line 2114, in _extract_member
os.makedirs(upperdirs)
File "/auto/pysw/cel8x/python64/3.6.10/lib/python3.6/os.py", line 210, in makedirs
makedirs(head, mode, exist_ok)
File "/auto/pysw/cel8x/python64/3.6.10/lib/python3.6/os.py", line 220, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/bootflash'
我正在指定要提取的文件夹,但它似乎仍在尝试在根目录中创建一个文件夹。这种行为是预期的吗?我可以在文件管理器中很好地提取这个 tar 文件。在 python 中有没有办法处理这个问题?
I am specifying the folder to extract but still it seems trying to create a folder in the root directory. is this behavior expected?
是的。提供的路径只是一个“当前目录”,而不是“监狱”。文档甚至特别警告它:
It is possible that files are created outside of path, e.g. members that have absolute filenames starting with "/"
or filenames with two dots ".."
.
I can extract this tar file fine in file manager. is there a way to handle this in python?
使用extractfile
获取归档文件内容的伪文件句柄,然后将其复制到任何你想要的地方。
Python代码:
import tarfile,os
import sys
def extract_system_report (tar_file_path):
extract_path = ""
tar = tarfile.open(sys.argv[1])
for member in tar.getmembers():
print ("member_name is: ")
print (member.name)
tar.extract(member.name, "./crachinfo/")
extract_system_report(sys.argv[1])
提取文件时出现以下错误:
>> python tar_read.py /nobackup/deepakhe/POLARIS_POJECT_09102019/hackathon_2021/a.tar.gz
member_name is:
/bootflash/.prst_sync/reload_info
Traceback (most recent call last):
File "tar_read.py", line 38, in <module>
extract_system_report(sys.argv[1])
File "tar_read.py", line 10, in extract_system_report
tar.extract(member.name, "./crachinfo/")
File "/auto/pysw/cel8x/python64/3.6.10/lib/python3.6/tarfile.py", line 2052, in extract
numeric_owner=numeric_owner)
File "/auto/pysw/cel8x/python64/3.6.10/lib/python3.6/tarfile.py", line 2114, in _extract_member
os.makedirs(upperdirs)
File "/auto/pysw/cel8x/python64/3.6.10/lib/python3.6/os.py", line 210, in makedirs
makedirs(head, mode, exist_ok)
File "/auto/pysw/cel8x/python64/3.6.10/lib/python3.6/os.py", line 220, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/bootflash'
我正在指定要提取的文件夹,但它似乎仍在尝试在根目录中创建一个文件夹。这种行为是预期的吗?我可以在文件管理器中很好地提取这个 tar 文件。在 python 中有没有办法处理这个问题?
I am specifying the folder to extract but still it seems trying to create a folder in the root directory. is this behavior expected?
是的。提供的路径只是一个“当前目录”,而不是“监狱”。文档甚至特别警告它:
It is possible that files are created outside of path, e.g. members that have absolute filenames starting with
"/"
or filenames with two dots".."
.
I can extract this tar file fine in file manager. is there a way to handle this in python?
使用extractfile
获取归档文件内容的伪文件句柄,然后将其复制到任何你想要的地方。