将所需文件移动到需要的位置

发布于 2024-11-02 06:42:43 字数 616 浏览 3 评论 0原文

我正在编写一个 Python 脚本,用于解压缩压缩目录并安装一些文件。当您解压压缩文件时,可能会出现诸如 ./usr/bin/package./var/lib/package 之类的文件。解压后的目录包含它们实际需要去的目录的名称。假设目录结构如下:


./zippedfile.zip
./usr
    /bin
        /package
./var
    ./lib
        ./package
Now I need to move the required files where they need to go. For example I need to move ./usr/bin/package to the actual /usr/bin/ directory. Thing is, it's not predictable which folders there may be, so the script needs to find the required directories and move them. Thanks, hopefully I made the question clear.

I'm working on a Python script that unzips a compressed directory and installs some files. When you unzip the compressed file, there could be files like ./usr/bin/package or ./var/lib/package. The unzipped directory contains the names of the dirs where they actually need to go. Lets say the directory structure looks like:


./zippedfile.zip
./usr
    /bin
        /package
./var
    ./lib
        ./package


Now I need to move the required files where they need to go. For example I need to move ./usr/bin/package to the actual /usr/bin/ directory. Thing is, it's not predictable which folders there may be, so the script needs to find the required directories and move them. Thanks, hopefully I made the question clear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里梦着梦中梦 2024-11-09 06:42:43

使用 zipfile 模块读取 zip 文件并使用 extract(name, dest) 提取每个文件。例如:

import sys
import zipfile

def unzip(path, root='/'):
    zh = zipfile.ZipFile(path, 'r')
    for name in zh.namelist():
        zh.extract(name, root)
    zh.close()

unzip(sys.argv[1])

如果 zip 中的文件的路径为 ./usr/bin/script,它将被提取到 /usr/bin/script

用法:

% python unzip.py myfiles.zip

更新:要处理 tars,请使用 tarfile 模块 。这使用文件扩展名(gzip、bzip2)检测压缩类型:

import os
import sys
import tarfile

def untar(path, root='/'):
    mode = 'r'
    _, ext = os.path.splitext(path)
    if ext in ('.gz', '.bz2'):
        mode += ':' + ext[1:]
    th = tarfile.open(path, mode)
    for info in th.getmembers():
        th.extract(info, root)
    th.close()

untar(sys.argv[1])

更新 如果您无法使用 zipfile 或 tarfile 模块,您可以调用相应的系统命令来获得相同的结果:

import os
import subprocess
import sys

def untar(path, root):
    subprocess.call(['tar', '-C', root, '-xf', path])

def unzip(path, root):
    subprocess.call(['unzip', os.path.abspath(path)], cwd=root)

path = sys.argv[1]
dest = os.path.abspath(sys.argv[2])
if not os.path.exists(dest):
    os.makedirs(dest)
if path.endswith('.zip'):
    unzip(path, dest)
else:
    untar(path, dest)

Use the zipfile module to read the zip file and extract(name, dest) to extract each file. For example:

import sys
import zipfile

def unzip(path, root='/'):
    zh = zipfile.ZipFile(path, 'r')
    for name in zh.namelist():
        zh.extract(name, root)
    zh.close()

unzip(sys.argv[1])

If a file within the zip has a path of ./usr/bin/script it will be extracted to /usr/bin/script.

Usage:

% python unzip.py myfiles.zip

Update: To handle tars, use the tarfile module. This detects the compression type using the file extension (gzip, bzip2):

import os
import sys
import tarfile

def untar(path, root='/'):
    mode = 'r'
    _, ext = os.path.splitext(path)
    if ext in ('.gz', '.bz2'):
        mode += ':' + ext[1:]
    th = tarfile.open(path, mode)
    for info in th.getmembers():
        th.extract(info, root)
    th.close()

untar(sys.argv[1])

Updated If you can't use the zipfile or tarfile modules, you can invoke corresponding system commands to get the same results:

import os
import subprocess
import sys

def untar(path, root):
    subprocess.call(['tar', '-C', root, '-xf', path])

def unzip(path, root):
    subprocess.call(['unzip', os.path.abspath(path)], cwd=root)

path = sys.argv[1]
dest = os.path.abspath(sys.argv[2])
if not os.path.exists(dest):
    os.makedirs(dest)
if path.endswith('.zip'):
    unzip(path, dest)
else:
    untar(path, dest)
丶视觉 2024-11-09 06:42:43

据我了解,您需要的大部分内容都在标准库的 os.path 模块中。那里有一些有用的函数:

如果您需要调用 shell 命令来实际移动目录,您可以使用 subprocess 模块 (请参阅此处的文档)也在标准库中。

希望这有帮助。

As I understand it, pretty much of what you need is in the os.path module in the standard library. Some useful functions there:

  • os.path.exists: Return True if path refers to an existing path. Returns False for broken symbolic links. On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.

  • os.path.isdir: Return True if path is an existing directory. This follows symbolic links, so both islink() and isdir() can be true for the same path.

If you need to invoke shell commands to actually move the directories, you can use the subprocess module (see documentation here) also in the standard library.

Hope this helps.

情何以堪。 2024-11-09 06:42:43

import zipfile, os
zipped = zipfile.ZipFile('zippedfile.zip')
os.chdir('/')
zipped.extractall()

import zipfile, os
zipped = zipfile.ZipFile('zippedfile.zip')
os.chdir('/')
zipped.extractall()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文