将所需文件移动到需要的位置
我正在编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 zipfile 模块读取 zip 文件并使用
extract(name, dest)
提取每个文件。例如:如果 zip 中的文件的路径为
./usr/bin/script
,它将被提取到/usr/bin/script
。用法:
更新:要处理 tars,请使用 tarfile 模块 。这使用文件扩展名(gzip、bzip2)检测压缩类型:
更新 如果您无法使用 zipfile 或 tarfile 模块,您可以调用相应的系统命令来获得相同的结果:
Use the zipfile module to read the zip file and
extract(name, dest)
to extract each file. For example:If a file within the zip has a path of
./usr/bin/script
it will be extracted to/usr/bin/script
.Usage:
Update: To handle tars, use the tarfile module. This detects the compression type using the file extension (gzip, bzip2):
Updated If you can't use the zipfile or tarfile modules, you can invoke corresponding system commands to get the same results:
据我了解,您需要的大部分内容都在标准库的 os.path 模块中。那里有一些有用的函数:
os.path.exists:如果路径引用现有路径,则返回 True。对于损坏的符号链接返回 False。在某些平台上,如果未授予对请求的文件执行 os.stat() 的权限,则该函数可能会返回 False,即使路径实际存在。
os.path.isdir:返回 True如果路径是现有目录。这遵循符号链接,因此 islink() 和 isdir() 对于同一路径都可以为 true。
如果您需要调用 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.