这是我的代码正文:
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/