简体   繁体   中英

Python script is behaving differently when run through batch file and cmd line

I have a Python script which needs to be run with a batch file, but I've been using cmd to test it. When run through cmd, it works fine. However, the script seems to behave differently when run through the batch file. I've isolated the section of code which seems to be the problem:

CRFOLDER = "some path to all my files"

isReady = False
os.startfile(os.path.join(CRFOLDER,"CLogger.exe"))

while not isReady:

    try:
        open(os.path.join(CRFOLDER,"CRPYLog.py"))
        isReady = True
    except:
        print "Not ready yet"
        time.sleep(0.25)

import CRPYLog as PyLog

This code calls an executable which creates a Python file, which I then import (if you're curious about the reason, check here ). As I said, this works fine when run through cmd. However, when I use the batch file, the while loop runs infinitely (or at least for 2 minutes, when run through cmd it hits the except just once). This is weird. I checked this , this , and this question with no luck. The batch file is below.

start Y:\Admin\Anaconda\python.exe "Y:\Projects\Advent - Overhead Projects\Copy - ADV001 - CR Records Management - Copy\Python\CRWizard.py" 

I've tried pasting that exact command into cmd and it worked fine, but the batch file does not. Thanks in advance

Probably, your batch file is running excutable with windows native call ( os.startfile ), and hence sets some defaults (current folder), forcing executable to create resulting file in folder that differs from expected CRFOLDER .

I suggest you to specify excplicetely in which foder you need this .py file to be created, for example, to be a flexible solution, as a paramether to executable.

You don't have to type the start command for that. See this . Just remove the start command.

Y:\Admin\Anaconda\python.exe "Y:\Projects\Advent - Overhead Projects\Copy - ADV001 - CR Records Management - Copy\Python\CRWizard.py"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM