使用 zipfile 解压缩后文件权限丢失
File permissions lost after unzipping with zipfile
我在 Ubuntu 中提取了一个 zip 文件(hisat2-2.2.0-Linux_x86_64.zip
来自 https://cloud.biohpc.swmed.edu/index.php/s/hisat2-220-Linux_x86_64/download)(通过右键单击该文件并选择此处提取),这为我提供了以下文件权限:
total 91M
-rw-r--r-- 1 user user 584 Jul 9 2019 NEWS
-rw-r--r-- 1 user user 78K Jul 9 2019 MANUAL.markdown
-rw-r--r-- 1 user user 35K Jul 9 2019 LICENSE
-rwxr-xr-x 1 user user 2.8K Jul 9 2019 hisat2-build
-rw-r--r-- 1 user user 1.3K Jul 9 2019 AUTHORS
-rw-r--r-- 1 user user 202 Jan 23 2020 TUTORIAL
-rw-r--r-- 1 user user 64K Jan 23 2020 MANUAL
-rw-r--r-- 1 user user 6 Dec 2 2020 VERSION
-rwxr-xr-x 1 user user 35K Dec 2 2020 hisat2_simulate_reads.py
-rwxr-xr-x 1 user user 5.6K Dec 2 2020 hisat2_read_statistics.py
-rwxr-xr-x 1 user user 2.7K Dec 2 2020 hisat2-inspect
-rwxr-xr-x 1 user user 5.0K Dec 2 2020 hisat2_extract_splice_sites.py
-rwxr-xr-x 1 user user 35K Dec 2 2020 hisat2_extract_snps_haplotypes_VCF.py
-rwxr-xr-x 1 user user 19K Dec 2 2020 hisat2_extract_snps_haplotypes_UCSC.py
-rwxr-xr-x 1 user user 5.5K Dec 2 2020 hisat2_extract_exons.py
-rwxr-xr-x 1 user user 20K Dec 2 2020 hisat2
-rwxr-xr-x 1 user user 5.0K Dec 2 2020 extract_splice_sites.py
-rwxr-xr-x 1 user user 5.5K Dec 2 2020 extract_exons.py
-rwxr-xr-x 1 user user 5.3M Dec 2 2020 hisat2-build-s
-rwxr-xr-x 1 user user 5.3M Dec 2 2020 hisat2-build-l
-rwxr-xr-x 1 user user 15M Dec 2 2020 hisat2-align-s
-rwxr-xr-x 1 user user 15M Dec 2 2020 hisat2-align-l
-rwxr-xr-x 1 user user 3.5M Dec 2 2020 hisat2-inspect-s
-rwxr-xr-x 1 user user 3.5M Dec 2 2020 hisat2-inspect-l
-rwxr-xr-x 1 user user 6.7M Dec 2 2020 hisat2-repeat
-rwxr-xr-x 1 user user 3.7M Dec 2 2020 hisat2-build-s-debug
-rwxr-xr-x 1 user user 3.6M Dec 2 2020 hisat2-build-l-debug
-rwxr-xr-x 1 user user 11M Dec 2 2020 hisat2-align-s-debug
-rwxr-xr-x 1 user user 11M Dec 2 2020 hisat2-align-l-debug
-rwxr-xr-x 1 user user 1.8M Dec 2 2020 hisat2-inspect-s-debug
-rwxr-xr-x 1 user user 1.7M Dec 2 2020 hisat2-inspect-l-debug
-rwxr-xr-x 1 user user 5.2M Dec 2 2020 hisat2-repeat-debug
drwxr-xr-x 2 user user 4.0K Dec 2 2020 scripts
drwxr-xr-x 5 user user 4.0K Dec 2 2020 example
当我使用 Python 解压缩此文件时:
from zipfile import ZipFile
download_file = "hisat2-2.2.0-Linux_x86_64.zip"
with ZipFile(download_file, 'r') as zip_ref:
zip_ref.extractall(script_dir)
从Ubuntu:
中解压出来的文件权限不一样(我失去了执行文件的权限)
total 91M
drwxrwxr-x 2 user user 4.0K Aug 18 23:15 scripts
drwxrwxr-x 2 user user 4.0K Aug 18 23:15 hisatgenotype_scripts
drwxrwxr-x 2 user user 4.0K Aug 18 23:15 hisatgenotype_modules
drwxrwxr-x 5 user user 4.0K Aug 18 23:15 example
-rw-rw-r-- 1 user user 5.0K Aug 18 23:25 hisat2_extract_splice_sites.py
-rw-rw-r-- 1 user user 35K Aug 18 23:25 hisat2_extract_snps_haplotypes_VCF.py
-rw-rw-r-- 1 user user 122K Aug 18 23:25 hisatgenotype_locus.py
-rw-rw-r-- 1 user user 21K Aug 18 23:25 hisatgenotype_build_genome.py
-rw-rw-r-- 1 user user 4.8K Aug 18 23:25 hisat2_read_statistics.py
-rw-rw-r-- 1 user user 19K Aug 18 23:25 hisat2_extract_snps_haplotypes_UCSC.py
-rw-rw-r-- 1 user user 5.0K Aug 18 23:25 extract_splice_sites.py
-rw-rw-r-- 1 user user 18K Aug 18 23:25 hisatgenotype.py
-rw-rw-r-- 1 user user 73K Aug 18 23:25 hisatgenotype_hla_cyp.py
-rw-rw-r-- 1 user user 52K Aug 18 23:25 hisatgenotype_extract_vars.py
-rw-rw-r-- 1 user user 23K Aug 18 23:25 hisatgenotype_extract_reads.py
-rw-rw-r-- 1 user user 34K Aug 18 23:25 hisat2_simulate_reads.py
-rw-rw-r-- 1 user user 5.5K Aug 18 23:25 hisat2_extract_exons.py
-rw-rw-r-- 1 user user 5.5K Aug 18 23:25 extract_exons.py
-rw-rw-r-- 1 user user 6 Aug 18 23:25 VERSION
-rw-rw-r-- 1 user user 202 Aug 18 23:25 TUTORIAL
-rw-rw-r-- 1 user user 584 Aug 18 23:25 NEWS
-rw-rw-r-- 1 user user 78K Aug 18 23:25 MANUAL.markdown
-rw-rw-r-- 1 user user 64K Aug 18 23:25 MANUAL
-rw-rw-r-- 1 user user 35K Aug 18 23:25 LICENSE
-rw-rw-r-- 1 user user 2.7K Aug 18 23:25 hisat2-inspect
-rw-rw-r-- 1 user user 2.8K Aug 18 23:25 hisat2-build
-rwxrwxr-x 1 user user 20K Aug 18 23:25 hisat2
-rw-rw-r-- 1 user user 1.3K Aug 18 23:25 AUTHORS
-rw-rw-r-- 1 user user 5.5M Aug 18 23:25 hisat2-build-s
-rw-rw-r-- 1 user user 5.5M Aug 18 23:25 hisat2-build-l
-rw-rw-r-- 1 user user 15M Aug 18 23:25 hisat2-align-s
-rw-rw-r-- 1 user user 15M Aug 18 23:25 hisat2-align-l
-rw-rw-r-- 1 user user 3.4M Aug 18 23:25 hisat2-inspect-s
-rw-rw-r-- 1 user user 3.4M Aug 18 23:25 hisat2-inspect-l
-rw-rw-r-- 1 user user 6.8M Aug 18 23:25 hisat2-repeat
-rw-rw-r-- 1 user user 3.5M Aug 18 23:25 hisat2-build-s-debug
-rw-rw-r-- 1 user user 3.4M Aug 18 23:25 hisat2-build-l-debug
-rw-rw-r-- 1 user user 11M Aug 18 23:25 hisat2-align-s-debug
-rw-rw-r-- 1 user user 11M Aug 18 23:25 hisat2-align-l-debug
-rw-rw-r-- 1 user user 1.7M Aug 18 23:25 hisat2-inspect-s-debug
-rw-rw-r-- 1 user user 1.7M Aug 18 23:25 hisat2-inspect-l-debug
-rw-rw-r-- 1 user user 5.1M Aug 18 23:25 hisat2-repeat-debug
如何使用 Python 命令保持原始文件权限?
Python 的 zipfile
不支持 Zip 文件中的 Unix 样式权限,因为它们是 non standard。您可以通过 subprocess
模块调用 unzip
命令:
import subprocess
subprocess.run(["unzip", download_file, "-d", script_dir])
我在 Ubuntu 中提取了一个 zip 文件(hisat2-2.2.0-Linux_x86_64.zip
来自 https://cloud.biohpc.swmed.edu/index.php/s/hisat2-220-Linux_x86_64/download)(通过右键单击该文件并选择此处提取),这为我提供了以下文件权限:
total 91M
-rw-r--r-- 1 user user 584 Jul 9 2019 NEWS
-rw-r--r-- 1 user user 78K Jul 9 2019 MANUAL.markdown
-rw-r--r-- 1 user user 35K Jul 9 2019 LICENSE
-rwxr-xr-x 1 user user 2.8K Jul 9 2019 hisat2-build
-rw-r--r-- 1 user user 1.3K Jul 9 2019 AUTHORS
-rw-r--r-- 1 user user 202 Jan 23 2020 TUTORIAL
-rw-r--r-- 1 user user 64K Jan 23 2020 MANUAL
-rw-r--r-- 1 user user 6 Dec 2 2020 VERSION
-rwxr-xr-x 1 user user 35K Dec 2 2020 hisat2_simulate_reads.py
-rwxr-xr-x 1 user user 5.6K Dec 2 2020 hisat2_read_statistics.py
-rwxr-xr-x 1 user user 2.7K Dec 2 2020 hisat2-inspect
-rwxr-xr-x 1 user user 5.0K Dec 2 2020 hisat2_extract_splice_sites.py
-rwxr-xr-x 1 user user 35K Dec 2 2020 hisat2_extract_snps_haplotypes_VCF.py
-rwxr-xr-x 1 user user 19K Dec 2 2020 hisat2_extract_snps_haplotypes_UCSC.py
-rwxr-xr-x 1 user user 5.5K Dec 2 2020 hisat2_extract_exons.py
-rwxr-xr-x 1 user user 20K Dec 2 2020 hisat2
-rwxr-xr-x 1 user user 5.0K Dec 2 2020 extract_splice_sites.py
-rwxr-xr-x 1 user user 5.5K Dec 2 2020 extract_exons.py
-rwxr-xr-x 1 user user 5.3M Dec 2 2020 hisat2-build-s
-rwxr-xr-x 1 user user 5.3M Dec 2 2020 hisat2-build-l
-rwxr-xr-x 1 user user 15M Dec 2 2020 hisat2-align-s
-rwxr-xr-x 1 user user 15M Dec 2 2020 hisat2-align-l
-rwxr-xr-x 1 user user 3.5M Dec 2 2020 hisat2-inspect-s
-rwxr-xr-x 1 user user 3.5M Dec 2 2020 hisat2-inspect-l
-rwxr-xr-x 1 user user 6.7M Dec 2 2020 hisat2-repeat
-rwxr-xr-x 1 user user 3.7M Dec 2 2020 hisat2-build-s-debug
-rwxr-xr-x 1 user user 3.6M Dec 2 2020 hisat2-build-l-debug
-rwxr-xr-x 1 user user 11M Dec 2 2020 hisat2-align-s-debug
-rwxr-xr-x 1 user user 11M Dec 2 2020 hisat2-align-l-debug
-rwxr-xr-x 1 user user 1.8M Dec 2 2020 hisat2-inspect-s-debug
-rwxr-xr-x 1 user user 1.7M Dec 2 2020 hisat2-inspect-l-debug
-rwxr-xr-x 1 user user 5.2M Dec 2 2020 hisat2-repeat-debug
drwxr-xr-x 2 user user 4.0K Dec 2 2020 scripts
drwxr-xr-x 5 user user 4.0K Dec 2 2020 example
当我使用 Python 解压缩此文件时:
from zipfile import ZipFile
download_file = "hisat2-2.2.0-Linux_x86_64.zip"
with ZipFile(download_file, 'r') as zip_ref:
zip_ref.extractall(script_dir)
从Ubuntu:
中解压出来的文件权限不一样(我失去了执行文件的权限)total 91M
drwxrwxr-x 2 user user 4.0K Aug 18 23:15 scripts
drwxrwxr-x 2 user user 4.0K Aug 18 23:15 hisatgenotype_scripts
drwxrwxr-x 2 user user 4.0K Aug 18 23:15 hisatgenotype_modules
drwxrwxr-x 5 user user 4.0K Aug 18 23:15 example
-rw-rw-r-- 1 user user 5.0K Aug 18 23:25 hisat2_extract_splice_sites.py
-rw-rw-r-- 1 user user 35K Aug 18 23:25 hisat2_extract_snps_haplotypes_VCF.py
-rw-rw-r-- 1 user user 122K Aug 18 23:25 hisatgenotype_locus.py
-rw-rw-r-- 1 user user 21K Aug 18 23:25 hisatgenotype_build_genome.py
-rw-rw-r-- 1 user user 4.8K Aug 18 23:25 hisat2_read_statistics.py
-rw-rw-r-- 1 user user 19K Aug 18 23:25 hisat2_extract_snps_haplotypes_UCSC.py
-rw-rw-r-- 1 user user 5.0K Aug 18 23:25 extract_splice_sites.py
-rw-rw-r-- 1 user user 18K Aug 18 23:25 hisatgenotype.py
-rw-rw-r-- 1 user user 73K Aug 18 23:25 hisatgenotype_hla_cyp.py
-rw-rw-r-- 1 user user 52K Aug 18 23:25 hisatgenotype_extract_vars.py
-rw-rw-r-- 1 user user 23K Aug 18 23:25 hisatgenotype_extract_reads.py
-rw-rw-r-- 1 user user 34K Aug 18 23:25 hisat2_simulate_reads.py
-rw-rw-r-- 1 user user 5.5K Aug 18 23:25 hisat2_extract_exons.py
-rw-rw-r-- 1 user user 5.5K Aug 18 23:25 extract_exons.py
-rw-rw-r-- 1 user user 6 Aug 18 23:25 VERSION
-rw-rw-r-- 1 user user 202 Aug 18 23:25 TUTORIAL
-rw-rw-r-- 1 user user 584 Aug 18 23:25 NEWS
-rw-rw-r-- 1 user user 78K Aug 18 23:25 MANUAL.markdown
-rw-rw-r-- 1 user user 64K Aug 18 23:25 MANUAL
-rw-rw-r-- 1 user user 35K Aug 18 23:25 LICENSE
-rw-rw-r-- 1 user user 2.7K Aug 18 23:25 hisat2-inspect
-rw-rw-r-- 1 user user 2.8K Aug 18 23:25 hisat2-build
-rwxrwxr-x 1 user user 20K Aug 18 23:25 hisat2
-rw-rw-r-- 1 user user 1.3K Aug 18 23:25 AUTHORS
-rw-rw-r-- 1 user user 5.5M Aug 18 23:25 hisat2-build-s
-rw-rw-r-- 1 user user 5.5M Aug 18 23:25 hisat2-build-l
-rw-rw-r-- 1 user user 15M Aug 18 23:25 hisat2-align-s
-rw-rw-r-- 1 user user 15M Aug 18 23:25 hisat2-align-l
-rw-rw-r-- 1 user user 3.4M Aug 18 23:25 hisat2-inspect-s
-rw-rw-r-- 1 user user 3.4M Aug 18 23:25 hisat2-inspect-l
-rw-rw-r-- 1 user user 6.8M Aug 18 23:25 hisat2-repeat
-rw-rw-r-- 1 user user 3.5M Aug 18 23:25 hisat2-build-s-debug
-rw-rw-r-- 1 user user 3.4M Aug 18 23:25 hisat2-build-l-debug
-rw-rw-r-- 1 user user 11M Aug 18 23:25 hisat2-align-s-debug
-rw-rw-r-- 1 user user 11M Aug 18 23:25 hisat2-align-l-debug
-rw-rw-r-- 1 user user 1.7M Aug 18 23:25 hisat2-inspect-s-debug
-rw-rw-r-- 1 user user 1.7M Aug 18 23:25 hisat2-inspect-l-debug
-rw-rw-r-- 1 user user 5.1M Aug 18 23:25 hisat2-repeat-debug
如何使用 Python 命令保持原始文件权限?
Python 的 zipfile
不支持 Zip 文件中的 Unix 样式权限,因为它们是 non standard。您可以通过 subprocess
模块调用 unzip
命令:
import subprocess
subprocess.run(["unzip", download_file, "-d", script_dir])