简体   繁体   中英

When I open a batch File that is supossed to create another file with python, python opens it but it doesn't create the file

import os
import time


def FindTask():
    while True:
        os.startfile('C:\\Users\\Joze\\Desktop\\League Bot\\TaskFinder.bat')
        time.sleep(2)
        from errorlevel import h
        if h == 0:
            print("is")
        elif h == 1:
            print("no")

FindTask()    

TaskFinder.bat is supossed to create a file named errorlevel.py .

The debugger does not show any errors so the file is being opened, but it doesn't create a file.

I have tried using subprocess but didn't work either.

Inside the batch file there is:

@tasklist | find /i "WinRAR.exe"
@echo h = %errorlevel% > errorlevel.py

You should be able to directly use

cp = subprocess.run('tasklist | find /i "WinRAR.exe"', shell=True)
print(cp.returncode)

instead of calling the batch script.

Even better would be not to shell out at all but use eg the psutil library to find the process(es) directly in Python.

When I executed the same code it worked for me, I am using python 3, maybe this is the problem.

If it's not that I am not sure that I know why TaskFinder.bat is not opening for you a new file, but I bet it is something in the Operating System, it is probably this because it's not python.

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