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

标签 python linux

所以我的脚本是当我在 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()

这是脚本的输出:

Conky: forked to background, pid is 18519

i3-msg -t get_workspaces 的示例输出(如果我打开了 2 个工作区):

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/dzconky.sh。 ~/bin/conkyrc_cli:

# 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()

~/bin/dzconky.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

编辑:更新代码以反射(reflect)新变化和新输出

最佳答案

你在所有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_for_3_workspaces.sh

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

关于python - 如何正确使用 python 访问系统命令并使我的脚本工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17517188/

相关文章:

python - Groupby 名称用所有列中的最大值替换值 pandas

python - 仅使用抽象列表函数将字符串中的每个字母与其在字符串中的位置(从 1 开始)相乘

PHP->Python JSON 问题

linux - 如何在 Makefile 中使用 chroot?

python - 如何在 PyCharm 项目 View 中隐藏 venv

python - PostgreSQL:plpython3u 中具有单值的集合返回函数

c++ - 在 linux/sys/class/gpio 中写入文件的错误

linux - 在 Cubietruck 上安装 node.js 和 mongoDB

linux - 在 Linux 上的脚本中使用 azcopy 时如何避免出现提示?

php - 将 PHP 用于守护进程是否明智?