I’m using a [script] program that works under Python 3.5.2, but not under 3.12.3. From my research, subprocess.check_output() was changed in the 3.12.3 library, and the syntax differs from earlier versions. I program in many languages, but not Python, where I’m a total zero. I use Ubuntu, and recently updated to version 24.04. In this new OS release, Python was upgraded to 3.12.3, which broke my program.
This script is supposed to run in the background on Ubuntu as a “Startup” program. It’s purpose is to allow a “grid” view of the files in folders I select, while other folders are the regular “list” view. The folder view cannot be set on a folder-by-folder basis. This script resolves that issue.
Oddly, under Ubuntu 24.04, the startup script won’t start. However, I can open an terminal window and invoke the program manually where it may run for a few minutes, or error out immediately. Ultimately, errors cause the program to stop running.
Here is the code I believe generates errors under 3.12.3, but worked perfectly with 3.5.2:
pid = subprocess.check_output([“pidof”, “nautilus”]).decode(“utf-8”).strip()
I’d like to know how (or what) to change in the above program make it compatible with 3.12.3. Also, I don’t want to risk downgrading to an earlier version of Python lest it affect something else in the operating system.
One more detail: The program won’t start using the “Startup Programs”, but WILL run (sort of, with errors) if run manually from a terminal window. Everything was ported from an earlier version of Ubuntu. All permissions and paths have been checked.
Here is the entire program:
#!/usr/bin/env python3
import subprocess
import time
import os
wlist = os.environ["HOME"]+"/.window_list.txt"
def get(cmd):
try:
return subprocess.check_output(cmd).decode("utf-8")
except subprocess.CalledProcessError:
return ""
def check_window():
pid = subprocess.check_output(["pidof", "nautilus"]).decode("utf-8").strip()
wlist = get(["wmctrl", "-lp"]).splitlines()
front = get(["xdotool", "getactivewindow", "getwindowname"]).strip()
return (front, [w for w in wlist if all([pid in w, front in w])])
match1 = check_window()
while True:
time.sleep(1)
match2 = check_window()
if all([match2 != match1, match2[1] != []]):
w = match2[0]
try:
if w in open(wlist).read().splitlines():
cmd = "xdotool key Ctrl+2"
subprocess.Popen(["/bin/bash", "-c", cmd])
else:
cmd = "xdotool key Ctrl+1"
subprocess.Popen(["/bin/bash", "-c", cmd])
except FileNotFoundError:
pass
match1 = match2
----
Example of the errors:
XGetWindowProperty[_NET_ACTIVE_WINDOW] failed (code=1)
xdo_get_active_window reported an error
pidof: can't read from 100934/stat
Assistance appreciated. Thanks!
Dan