简体   繁体   English

在Python脚本中运行批处理文件时出现Windows错误2

[英]Windows Error 2 when running batch file in Python script

I saw several threads dedicated to this error, but none solved my problem. 我看到了几个专用于此错误的线程,但没有一个解决了我的问题。 Thought I'd post so folks can look at my code and maybe we can figure out another solution for everyone. 以为我会发布,以便人们可以查看我的代码,也许我们可以为每个人找出另一个解决方案。

I'm attempting to run a Python script whose first task is to run a batch file. 我正在尝试运行Python脚本,其首要任务是运行批处理文件。 The batch file actually runs Wget to download the files that the Python script will work on. 批处理文件实际上运行Wget来下载Python脚本将使用的文件。

If I run the entire Python script manually, it works perfectly. 如果我手动运行整个Python脚本,则可以完美运行。 However, if I run it using Windows Task Scheduler or from Command Line, it has issues with the batch script. 但是,如果我使用Windows Task Scheduler或从命令行运行它,则批处理脚本有问题。

If I comment out the batch script part, Task Scheduler/CMD can run the Python script just fine. 如果我批处理脚本部分,Task Scheduler / CMD可以很好地运行Python脚本。 Task Scheduler/CMD can also run the batch file independently with no issue. Task Scheduler / CMD也可以独立运行批处理文件,没有问题。

Here's my Python code: 这是我的Python代码:

import time
import os
import sys
from subprocess import Popen
import zipfile
import win32com.client as win32

#1# Run the downloader batch file
p = Popen("NDICDownloader.bat", cwd=r"C:\NDICUpdate")
stdout, stderr = p.communicate()
p.wait()

Here's the error I get in command line: 这是我在命令行中遇到的错误:

c:\Python27\python.exe c:\NDICUpdate\NDICUpdater.py
Traceback (most recent call last):
  file "c:\NDICUpdate\NDICUpdater.py", line 9, in (module)
    p = Popen("NDICDownloader.bat", cwd=r"C:\NDICUpdate")
  file "c:\Python27\lib\subprocess.py", line 672, in __init__
    errread, errwrite)
  file "c:\Python27\lib\subprocess.py", line 882, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

Here's the batch file: 这是批处理文件:

cd C:\NDICUpdate\NDIC

wget --arguments include sensitive info--

The batch file uses Wget to download files to the NDIC folder. 批处理文件使用Wget将文件下载到NDIC文件夹。 All scripts are located in the root folder C:\\NDICUpdate. 所有脚本都位于根文件夹C:\\ NDICUpdate中。 All files exist. 所有文件都存在。

The problem is trying to use Windows to run the batch file within the Python script. 问题是试图使用Windows在Python脚本中运行批处理文件。 Why are Windows and Python butting heads here?? 为什么Windows和Python碰到头了?

(Answered in the Comments. See Question with no answers, but issue solved in the comments (or extended in chat) ) (已在注释中回答。请参阅无答案的问题,但问题已在注释中解决(或在聊天中扩展)

@MC ND wrote: @MC ND写道:

Change your code to call cmd.exe as the started process with /c NDICUpdate.bat as parameters to the executable: 更改您的代码以使用/c NDICUpdate.bat作为可执行文件的参数来调用cmd.exe作为启动过程:

p = Popen(["cmd.exe", "/c NDICDownloader.bat"], cwd=r"C:\\NDICUpdate")

The OP wrote: OP写道:

that worked. 起作用了。 This also works: os.system("c:\\windows\\system32\\cmd.exe /c C:\\NDICDownloader.bat") . 这也适用: os.system("c:\\windows\\system32\\cmd.exe /c C:\\NDICDownloader.bat") What I ended up doing in the end was writing a batch file that runs the Wget and then runs the Python. 我最终要做的是编写一个批处理文件,该文件运行Wget ,然后运行Python。 Lots of headache/wasted time, but at least a workable solution. 很多头痛/浪费时间,但至少是一个可行的解决方案。 Still no idea how to get cmd.exe to run Python's subprocess. 仍然不知道如何获取cmd.exe来运行Python的子进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 放入批处理脚本时文件未运行 - Python File is not running when put in a batch script 在 python shell 从 Z0F4137ED1502B5045D6083AA258B 文件5C42Z 中运行 python 脚本 - Running python script in python shell from a windows batch file 通过批处理文件运行时,将python脚本的输出打印到Windows控制台 - Printing output of python script to Windows console, when running via batch file 通过批处理文件运行时,如何将 python 脚本的 output 打印到 Windows 控制台? - How to print output of python script to Windows console, when running via batch file? 运行Windows批处理文件,vbscript / wscript或Python控制台脚本时,将图像显示为初始屏幕 - Display an image as a splash screen when running Windows batch file, vbscript/wscript or Python console script 从 windows 批处理文件运行 python 脚本 - 文件名冲突 - Running python script from windows batch file - filename conflicts Windows 任务调度程序未运行 python 脚本/批处理文件 - Windows task scheduler not running python script/batch file 使用 ExecuteStreamCommand NIFI 通过 windows 批处理文件运行 python 脚本 - Running a python script through a windows batch file using ExecuteStreamCommand NIFI 使用python脚本运行批处理文件 - Running batch file with python script 从批处理文件运行 python 脚本时出现 ModuleNotFoundError - ModuleNotFoundError when running python script from a batch file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM