Python FileNotFoundError: [WinError 2] 系统找不到指定的文件。

用Idle运行Python脚本的时候发现如下错误:

Traceback (most recent call last):
  File "D:\Python\Python36-32\lib\site-packages\selenium-3.4.3-py3.6.egg\selenium\webdriver\common\service.py", line 74, in start
    stdout=self.log_file, stderr=self.log_file)
  File "D:\Python\Python36-32\lib\subprocess.py", line 707, in __init__
    restore_signals, start_new_session)
  File "D:\Python\Python36-32\lib\subprocess.py", line 990, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

后来在stackoverflow上找到如下解决办法:

In Windows , to use echo in subprocess, you would need to use shell=True . This is because echo is not a separate executable, but rather a built-in command for the windows command line. Example -
在windows 在子进程中使用echo,需要设置 shell =True,因为 echo 不是单独的命令,而是window CMD 内置的命令

process1 = subprocess.Popen(command1,stdout=subprocess.PIPE,shell=True)

Also, please do note, you should only use shell=True when absolutely necessary (as in this case , to use echo in windows in subprocess).

并且,需要注意,只有在绝对需要的情况下才使用shell=True

找到 subprocess.py脚本,将对应行加上shell=true 参数。

该问题解决。

### 解决 PythonFileNotFoundError WinError 2 文件到 错误 当遇到 `FileNotFoundError: [WinError 2]` 的错误时,这通常意味着尝试访问的文件不存在于指定位置。以下是几种可能的原因及相应的解决方案: #### 修改 subprocess.py 配置 对于某些特定情况下,修改 Python 源码中的配置可以解决问题。如果问题是由于调用外部命令引起的,则可以在 `subprocess.py` 文件中将参数 `shell=False` 改为 `shell=True`。例如,在 Python 3.7 版本下,此更改位于大约第 684 行的位置[^1]。 ```python import subprocess # 原始代码可能是这样的 result = subprocess.run(['command'], shell=False) # 更改后的代码应如下所示 result = subprocess.run(['command'], shell=True) ``` #### 处理 DLL 缺失问题 针对像 `'UIAutomationCore.dll'` 这样的动态链接库缺失情况,确保所有依赖项都已正确安装是非常重要的。检查并确认操作系统已经包含了必要的组件或更新了相关驱动程序和服务包[^2]。 #### 使用异常处理机制 为了使应用程序更加健壮,建议加入基本的异常捕获逻辑来优雅地应对潜在的问题。下面是一个简单的例子展示了如何利用 try-except 结构来进行此类操作[^3]: ```python try: with open('myfile.txt', 'r') as file_handle: content = file_handle.read() except FileNotFoundError: print("The specified file does not exist.") else: print(content) finally: pass # 可选清理工作 ``` #### MATLAB Engine 安装相关问题 如果是因 MATLAB Engine API 导致的类似错误,那么需要验证注册表键路径是否准确无误,并且确认 MATLAB 已经被适当地设置到了系统的环境变量里。此外,还需注意不同版本间的兼容性差异[^4].
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值