首页 > 解决方案 > 当我打开一个应该用 python 创建另一个文件的批处理文件时,python 会打开它但它不会创建文件

问题描述

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应该创建一个名为errorlevel.py.

调试器没有显示任何错误,因此文件正在打开,但它不会创建文件。

我试过使用subprocess但也没有工作。

在批处理文件中有:

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

标签: pythonbatch-file

解决方案


你应该可以直接使用

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

而不是调用批处理脚本。

更好的办法是根本不使用,而是使用例如psutil库直接在 Python 中查找进程。


推荐阅读