Сомнение при смене обоев с помощью скрипта Python

Вопрос

Я написал программу ниже, чтобы изменить обои. Он отлично работает, когда я запускаю его вручную. Однако, когда я перезагружу свой компьютер, он не изменит мои обои. Вместо этого он изменил фон экрана входа в систему ... Самое странное, что он отлично работает, когда я запускал только часть смены обоев при запуске.

Я использую Ubuntu16.04 с anaconda3. Эта программа отлично работает на VirtualBox с Ubuntu16.04 (без anaconda3) тоже ... Я переименовываю /anaconda3/bin/gsettings в gsettings_old, но он все еще не работает ...

Обновить

Недавно я запускаю ту же программу много раз, иногда она работает, иногда это не так. Однако я замечаю, что когда я меняю обои вручную, Ubuntu заметит меня, что исходные обои имеют тот же путь. Может быть, это причина, почему это не работает? Я не уверен ...

Код

#!/usr/bin/python3 import urllib.request import urllib.error import re import os import time import datetime def log(string): logPath = os.path.abspath(os.path.dirname(__file__)) + '/log' with open(logPath, 'a') as log: log.write(string + '\n') # wallpaper absolute path wallPaperPath = os.path.abspath(os.path.dirname(__file__)) + '/wallpaper.jpg' # simple setup # automatically run when login # warning: make sure there's a wallpaper.jpg after running this program for the first time # warning: otherwise it may continue to add command into ~/.profile if not os.path.exists(wallPaperPath): log('[SETUP]: ' + 'echo "' + os.path.abspath(__file__) + ' &" >> ~/.profile') os.system('echo "' + os.path.abspath(__file__) + ' &" >> ~/.profile') # run while True: # web resource getter while True: try: # open www.bing.com(actually http://www.bing.com/?mkt=zh-CN) with urllib.request.urlopen('http://www.bing.com/') as response: page = response.read().decode('utf-8') # extract the URL of wall paper urlPrefix = 'http://www.bing.com' patternString = 'g_img={url: "(.*?)"' pattern = re.compile(patternString) result = pattern.findall(page) wallPaperURL = urlPrefix + result[0] # get the wall paper with urllib.request.urlopen(wallPaperURL) as response: with open(wallPaperPath, "wb") as file: file.write(response.read()) log('[ RUN ]: Download Picture Done') break # exist the loop # when cannot connect to the internet except urllib.error.URLError as urlError: time.sleep(20) # wait 20 seconds log('[ERROR]: URLError') continue # continue the loop # other errors except Exception as e: time.sleep(20) # wait 20 seconds log('[ERROR]: Exception') continue # continue the loop # wall paper switcher # test current wallpaper sysWallpaper = os.popen('gsettings get org.gnome.desktop.background picture-uri').read().strip() configWallpaper = '\'file://' + wallPaperPath + '\'' # spare no effort to change the wallpaper :) while not sysWallpaper == configWallpaper: # change wallpaper through command line temp = os.popen('gsettings set org.gnome.desktop.background picture-uri "file://' + wallPaperPath + '"') log('[ RUN ]: ' + temp.read()) time.sleep(5) # test current wallpaper again sysWallpaper = os.popen('gsettings get org.gnome.desktop.background picture-uri').read().strip() log('[ RUN ]: Change Wallpaper Done') # clear cache time.sleep(3) os.system('rm ~/.cache/wallpaper/*') log('[ RUN ]: Clear Cache') # set next run time # never tested tomorrow = datetime.datetime.replace(datetime.datetime.now() + datetime.timedelta(days=1), hour=0, minute=0, second=0) delta = tomorrow - datetime.datetime.now() time.sleep(delta.seconds)
0
задан 19 October 2017 в 17:01

0 ответов

Другие вопросы по тегам:

Похожие вопросы: