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

2024-06-28 19:16:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我看到有几个线程专门针对这个错误,但没有一个解决我的问题。我想我会发帖让大家看看我的代码,也许我们可以为每个人找到另一个解决方案。在

我试图运行一个Python脚本,它的第一个任务是运行批处理文件。批处理文件实际上运行Wget来下载Python脚本要处理的文件。在

如果我手动运行整个Python脚本,它可以完美地工作。但是,如果我使用Windows任务调度器或从命令行运行它,则批处理脚本有问题。在

如果我注释掉批处理脚本部分,taskscheduler/CMD可以很好地运行Python脚本。Task Scheduler/CMD也可以独立运行批处理文件,没有问题。在

下面是我的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()

以下是我在命令行中遇到的错误:

^{pr2}$

批处理文件如下:

cd C:\NDICUpdate\NDIC

wget --arguments include sensitive info--

批处理文件使用Wget将文件下载到NDIC文件夹。所有脚本都位于根文件夹C:\NDICUpdate中。所有文件都存在。在

问题是试图使用Windows在Python脚本中运行批处理文件。为什么Windows和Python在这里碰头??在


Tags: 文件代码命令行import脚本文件夹cmdwindows
1条回答
网友
1楼 · 发布于 2024-06-28 19:16:15

(在评论中回答。见Question with no answers, but issue solved in the comments (or extended in chat)

@MC ND写道:

^{bq}$

评论写道:

that worked. This also works: 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. Lots of headache/wasted time, but at least a workable solution. Still no idea how to get cmd.exe to run Python's subprocess.

相关问题 更多 >