[英]How do I properly use python to access system commands and make my script work?
So the script I have is to have a larger dzen2 outputting conky on the screen when I have less than 3 workspaces open on i3 and shrink it back down when I have more than 3 workspaces open. 所以我的脚本是当我在i3上打开少于3个工作空间时,在屏幕上输出更大的dzen2 conky,当我打开3个以上的工作空间时将其缩小。 Here's the script dzresize.py: 这是脚本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()
Here's the output of the scrip: 这是脚本的输出:
Conky: forked to background, pid is 18519
sample output (if I have 2 workspaces open) for i3-msg -t get_workspaces: 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}]
Relies on i3, dzen2 and the files ~/bin/conkyrc_cli and ~/bin/dzconky.sh. 依赖于i3,dzen2和文件〜/ bin / conkyrc_cli和〜/ bin / dzconky.sh。 ~/bin/conkyrc_cli: 〜/斌/ 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: 〜/斌/ 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
Edit: updated to code to reflect new changes and new output 编辑:更新为代码以反映新的更改和新输出
You forgot ,
in all subprocess.call([...])
你忘了,
在所有subprocess.call([...])
["i3-msg", "-t", "get_workspaces"]
edit: 编辑:
if len(status_list) <=3:
subprocess.check_output(["dzconky_for_3_workspaces.sh"])
else:
subprocess.check_output(["dzconky.sh"])
dzconky_for_3_workspaces.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
You can use python's sh package. 你可以使用python的sh包。 It is very nice pythonic wrapper for shell execution. 它是用于shell执行的非常好的pythonic包装器。 If you take a look at the documentation you will find that sh
do probably most cases you need in a few lines. 如果你看一看的文件 ,你会发现, sh
你在几行需要大概大多数情况下。
Installation 安装
pip install sh
From docs: 来自docs:
easy as: 容易:
from sh import curl, git, ifconfig, ls ls() git('fetch') ifconfig(a=True)
redirections: 重定向:
ls(_out="files.list") ls("nonexistent", _err="error.txt") # there is also _in for stdin
piping: 管道:
# sort this directory by biggest file print(sort(du(glob("*"), "-sb"), "-rn"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.