Python 错误:找不到指定的文件 [英] Python error: Cannot find the file specified
问题描述
这是我的代码体:
os.chdir("C:\Users\Desktop")
rc = subprocess.call(['7z', 'a', 'test', '-y', 'myarchive.zip'] +
[r'device teams.txt'])
它给我一个错误,指向 r'device team.txt' 说指定的文件不存在.
It gives me an error pointing to r'device teams.txt' saying the specified file does not exist.
我检查了目录,它在桌面目录中,所以我不确定它为什么给我这个错误
I checked the directory and it is in the desktop directory so I am not sure why it is giving me this error
推荐答案
根据您的评论,问题不在于 txt
文件路径,而在于命令 7z
找不到.您可以通过调用 rc = subprocess.call(['7z'])
来检查这一点:错误 The system cannot find the file specified
仍然存在.
Based on your comments, the problem isn't the txt
file path, it's that the command 7z
cannot be found. You can check this by just calling rc = subprocess.call(['7z'])
: the error The system cannot find the file specified
persists.
以下是使用 PowerShell 实现相同目标的方法,例如:
Here's how you can achieve the same thing using PowerShell for example:
import os
import subprocess
os.chdir("C:\Users\Username\Desktop")
rc = subprocess.call("powershell Compress-Archive -Path 'device teams.txt' -DestinationPath archive.zip")
这篇关于Python 错误:找不到指定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!