如何正确使用python访问系统命令并使脚本正常工作?

2024-06-07 22:58:37 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我的脚本是在i3上打开少于3个工作区时,在屏幕上显示一个更大的dzen2输出conky,当我打开超过3个工作区时,将其缩小。这是剧本dzresize.py公司名称:

import subprocess
def main():
    #gets the number of workspaces from i3
    status = subprocess.check_output(["i3-msg", "-t", "get_workspaces"])
    #puts status in a list
    status_list = list(status)
    #sets name to 0
    name = 0
    for i in status_list:
        if i == "name":
            name +=1
    #counts the amount of name in status_list
    if len(status_list) <=3:
    #if the workspaces are less than or equal to 3, expands dzen2 with conky output
            subprocess.check_output(["conky", "-d", "-c", "~/bin/conkyrc_cli|dzen2", "-fg", "#666666", "-bg", "#333333", "-ta", "left", "-w", "725", "-x", "54", "-y", "750"])
    else:
    #if the workspaces are greater than or equal to 3 run the minimal smaller size dzen2 with conky
            subprocess.check_output(["dzconky.sh"])
main()

以下是纸条的输出:

^{pr2}$

i3 msg-t get_工作区的示例输出(如果我有两个工作区打开):

i3-msg -t get_workspaces
[{"num":1,"name":"1","visible":false,"focused":false,"rect":{"x":0,"y":0,"width":1366,"height":749},"#output":"LVDS1","urgent":false},{"num":2,"name":"2","visible":true,"focused":true,"rect":{"x":0,"y":#0,"width":1366,"height":749},"output":"LVDS1","urgent":false}]

依赖于i3、dzen2和~/bin/conkyrc_cli和~/bin文件/哲康基.sh. ~/bin/conkyrc_命令行:

# Conky configuration for Dzen2, to be piped into i3bar
##############################################
#  Settings
##############################################
background no
out_to_console yes
update_interval 1.0
total_run_times 0
use_spacer none
TEXT
^fg(\#6699cc)Processor^fg()^fg(green)${cpu cpu0}.00%^fg()^fg(white)|^fg(\#6699cc)Memory^fg()^fg(green)${memperc}.00%^fg()^fg(white)|^fg(\#6699cc)Root^fg()^fg(green)${fs_used_perc /}.00%^fg()^fg(white)|^fg(\#6699cc)Home^fg()^fg(green)${fs_used_perc /home}.00%^fg()^fg(white)|^fg(\#6699cc)Temperature^fg()^fg(green)${hwmon temp 1}'C^fg()^fg(white)|^fg(\#6699cc)Dn^fg()^fg(green)${downspeedf wlan0}KiB^fg()^fg(white)|^fg(\#6699cc)U^fg()^fg(green)${upspeedf wlan0}KiB^fg()

~/箱/哲康基.sh公司名称:

#!/bin/sh
exec conky -d -c "$HOME/bin/conkyrc_cli" | dzen2 -fg "#666666" -bg "#333333" -ta left -w 607 -x 188 -y 750 &
exit 0

编辑:更新到代码以反映新的更改和新的输出


Tags: thetonameoutputbinstatusgreenlist
2条回答

{a1}你可以使用python的包。对于shell执行来说,它是非常好的pythonic包装器。 如果你看一下documentation,你会发现sh可能在几行中就可以完成大多数需要的情况。在

安装

pip install sh

来自文档:

简单如:

^{pr2}$

重定向:

ls(_out="files.list")
ls("nonexistent", _err="error.txt")
# there is also _in for stdin

管道:

# sort this directory by biggest file
print(sort(du(glob("*"), "-sb"), "-rn"))

你把,全忘了subprocess.call([...])

["i3-msg", "-t", "get_workspaces"]

编辑:

if len(status_list) <=3:
    subprocess.check_output(["dzconky_for_3_workspaces.sh"])
else:
    subprocess.check_output(["dzconky.sh"])

dzconky_为_3_工作区.sh在

^{pr2}$

相关问题 更多 >

    热门问题