Python错误:找不到指定的文件 [英] Python error: Cannot find the file specified

查看:846
本文介绍了Python错误:找不到指定的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码体:

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

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

这给我一个错误,指向r'device groups.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'])来检查此错误:错误系统找不到指定的文件仍然存在。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆