![](/img/trans.png)
[英]How to run shell-commands in IPython? (Python Profiling GUI tools)
[英]Why I cannot run shell commands via PS gui in Python?
我正在嘗試通過 python 的 gui (PySimple) 在終端中運行 avrdude 命令......它沒有返回任何錯誤,但是,它也不起作用。 所以這里是問題:
最初的想法。
import PySimpleGUI as sg
import subprocess
layout = [
[sg.Text('Filename')],
[sg.Input('', key='filename'), sg.FileBrowse()],
[sg.Button('Ok'), sg.Button('Cancel')],
]
window = sg.Window('Test', layout)
while True:
event, values = window.read()
if event in ('Exit', None): break
#print(event, values)
if event == 'Ok':
print(event, values)
#run the terimanl code???
#subprocess.run('/Users/mu234/Desktop/myScript_sm-ir.txt')
subprocess.run('ls')
window.close()
經過幾天的評論(例如 MikeyB 等)和我的幾次討論,谷歌搜索,我已經移動了一點,並將在此處發布嘗試如何解決這個問題,但是,當文件被處理時,不知何故整個程序不起作用...
import subprocess
import sys
import PySimpleGUI as sg
sg.theme('Dark Blue 4')
def main():
layout = [
[sg.Output(size=(110,40), background_color='black', text_color='white')],
##[sg.T('Prompt> '), sg.Input(key='-IN-', do_not_clear=False)], #type in prompt
[sg.Text('Choose the script you want to process')],
[sg.Input('', key='filename'), sg.FileBrowse()], #choose a file you want to process with the script
#[sg.Button('Process'), sg.Button('Cancel'), sg.Button('Exit')]], #process the chosen file, else exit
[sg.Button('Process', bind_return_key=True), sg.Button('Exit')] ]
window = sg.Window('Realtime Shell Command Output', layout)
while True: # Event Loop
event, values = window.read()
# print(event, values)
if event in (sg.WIN_CLOSED, 'Exit'):
break
elif event == 'Process':
#print(event, values)
runCommand(cmd=values['filename'], window=window)
window.close()
def runCommand(cmd, timeout=None, window=None):
nop = None
""" run shell command
@param cmd: command to execute
@param timeout: timeout for command execution
@param window: the PySimpleGUI window that the output is going to (needed to do refresh on)
@return: (return code from command, command output)
"""
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = ''
for line in p.stdout:
line = line.decode(errors='replace' if (sys.version_info) < (3, 5) else 'backslashreplace').rstrip()
output += line
print(line)
window.refresh() if window else nop # yes, a 1-line if, so shoot me
retval = p.wait(timeout)
return (retval, output)
main()
GUI 在那里,用戶可以輕松瀏覽和選擇文件(腳本)並對其進行處理,但是,腳本以某種方式卡住了。 例如,這是我在 GUI 中得到的輸出:
/Users/mu234/Desktop/myScript_am-ir.txt: line 3: --: command not found
/Users/mu234/Desktop/myScript_am-ir.txt:第 5 行:avrdude:未找到命令
/Users/mu234/Desktop/myScript_am-ir.txt: line 9: --: command not found
/Users/mu234/Desktop/myScript_am-ir.txt:第 11 行:avrdude:找不到命令
/Users/mu234/Desktop/myScript_am-ir.txt: line 14: --: command not found
/Users/mu234/Desktop/myScript_am-ir.txt:第 16 行:avrdude:找不到命令
有用?
簡而言之,它不起作用。 我在想權限有問題嗎? 我在 OSX 上。 ,我猜問題在於訪問文件的權限......腳本並執行腳本中的命令? 一般來說,該腳本只有幾行代碼(查看這里可能的命令:https ://www.cs.ou.edu/~fagg/classes/general/atmel/avrdude.pdf )。
例如,如果只使用“echo ...”,它就可以正常工作,正如您在上面的打印中看到的那樣...無論如何,請查看代碼並評論可能出錯的地方。
當您使用subprocess.run(command)
它會將輸出寫入 sys.stdout(Linux 上的 /dev/stdout),這基本上是終端/控制台的輸出。
你可能想做更多這樣的事情
from subprocess import Popen, PIPE
proc = Popen(command.split(' '), stdin=PIPE, stdout=PIPE, stderr=PIPE) # spawns the process, but redirects stdin, stdout, and stderr from the terminal/console
proc_output = proc.communicate()[0].decode('utf-8') # reads stdout output and stores string into a variable
# Do something with proc_output in your GUI
另外我在您注釋掉的行中看到,您正在嘗試使用 .txt 文件生成一個進程? 如果這是一個腳本,您將需要更改文件擴展名(在 Windows 上需要,shebangs 對 linux 就足夠了)
PS 而不是使用 subprocess 模塊來運行你的腳本,你可以簡單地從你的腳本中導入函數並從不同的腳本/文件中調用這些函數。
谷歌和文檔是你的朋友
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.