Python错误: Cannot find the file specified

标签 python directory subprocess 7zip

这是我的代码正文:

os.chdir("C:\\Users\\Desktop")

rc = subprocess.call(['7z', 'a', 'test', '-y', 'myarchive.zip'] +
                     [r'device teams.txt'])

它给我一个指向 r'deviceteams.txt' 的错误,指出指定的文件不存在。

我检查了目录,它位于桌面目录中,所以我不确定为什么它给我这个错误

最佳答案

根据您的评论,问题不在于 txt 文件路径,而是找不到命令 7z。您只需调用rc = subprocess.call(['7z'])即可检查这一点:错误系统找不到指定的文件仍然存在。

例如,以下是使用 PowerShell 实现相同目标的方法:

import os
import subprocess
os.chdir("C:\\Users\\Username\\Desktop")
rc = subprocess.call("powershell Compress-Archive -Path 'device teams.txt' -DestinationPath archive.zip")

关于Python错误: Cannot find the file specified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55838168/

相关文章:

python - 每个 arg 具有多个值的 arg 解析器的最佳实践

python - Try-Except-Finally 语句的过时书籍描述

directory - 在哪里安装 32 位和 64 位混合应用程序包

python -/bin/sh : 1: Syntax error: word unexpected - Using os. system() 来自 Python

python - subprocess.Popen 在 WSL Linux 上花费太长时间

Python:并行运行子进程

python - 如何将列表列表转换为字节?

java - 如何从 java 类调用 python 方法?

php - 使用 php 在文本文件中搜索短语/单词

linux - 如何找到目录中所有文件的大小总和?