这是我的代码体:

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

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


它给我一个错误,指向r'device team.txt',表明指定的文件不存在。

我检查了目录,它在桌面目录中,所以我不确定为什么会出现此错误

最佳答案

根据您的评论,问题不在于txt文件路径,而是找不到命令7z。您可以通过仅调用rc = subprocess.call(['7z'])进行检查:错误The system cannot find the file specified仍然存在。

例如,以下是使用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 - Python错误:找不到指定的文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55838168/

10-11 22:27
查看更多