Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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. The batch file actually runs Wget to download the files that the Python script will work on.

If I run the entire Python script manually, it works perfectly. However, if I run it using Windows Task Scheduler or from Command Line, it has issues with the batch script.

If I comment out the batch script part, Task Scheduler/CMD can run the Python script just fine. Task Scheduler/CMD can also run the batch file independently with no issue.

Here's my Python code:

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. All scripts are located in the root folder C:\NDICUpdate. All files exist.

The problem is trying to use Windows to run the batch file within the Python script. Why are Windows and Python butting heads here??

share|improve this question
    
Just a silly question, have you tryied to run cmd.exe /c NDICUpdate.bat? –  MC ND Mar 11 at 17:30
    
yep! works just fine. –  Reb Mar 11 at 17:35
    
that is, if you mean running NDICDownloader.bat from command line... –  Reb Mar 11 at 17:40
    
no, i mean changing it in your code to call cmd.exe as the started process with /c NDICUpdate.bat as parameters to the executable. –  MC ND Mar 11 at 18:47
1  
p = Popen(["cmd.exe", "/c NDICDownloader.bat"], cwd=r"C:\NDICUpdate") –  MC ND Mar 11 at 19:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.