小编Pau*_*son的帖子

Python - 读取 Powershell 脚本输出,使用子进程,希望逐行接收标准输出

一直试图从运行了一点的 Powershell 脚本读取标准输出,根据它正在 ping 的计算机数量生成输出。

试图让数据流入文本框,但毕竟我已经尝试过了,我似乎只能让它一次提供所有输出。

一直试图不使用subprocess.communicate(),因为它似乎也一次提供所有输出。

这是代码:

from tkinter import *
import os
from subprocess import Popen, PIPE


window = Tk()
window.title( 'PowerShell Script Temp' )

frame = Frame(window)

fldrPath = r"C:/Users/firstname.lastname/Downloads/Powershell Development/Monthly Scans/"

listbox = Listbox(frame)
listbox.configure(width=50)

for name in os.listdir(fldrPath):
    listbox.insert('end', name)

def selection():
    fileList = listbox.curselection()
    for file in fileList:
        os.chdir(fldrPath)
        
        # Right here is the problematic section
        with Popen(["powershell.exe", '-File', fldrPath + '\\' + listbox.get(file)], stdout=PIPE, bufsize=1,
                   universal_newlines=True) as p:
            for line in …
Run Code Online (Sandbox Code Playgroud)

python powershell tkinter python-3.x

5
推荐指数
1
解决办法
428
查看次数

标签 统计

powershell ×1

python ×1

python-3.x ×1

tkinter ×1