Python 根据一天中的时间更改壁纸的脚本
Python script to change wallpaper depending on time of day
我正在使用 xfce 运行 Linux Mint 13。使用 this thread 中的脚本,我能够以这种格式运行 cronjob:
PATH=/usr/bin/python/:/usr/bin/python3/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# The Wallpaper Changer
0 * * * * /home/tessitura/8BitDay/set.py
...但我遇到了脚本本身的一些问题。照原样,只改变目录名称,它给了我以下错误:
Traceback (most recent call last):
File "./set.py", line 19, in <module>
os.rename('now.png', current)
OSError: [Errno 2] No such file or directory
我试过稍微调整一下代码,它有点管用;墙纸发生变化,但 now.png 最终被删除,导致 cronjob 运行时出现空白图像。这是我现在拥有的:
#!/usr/bin/python3
# Finds the current hour
import datetime
time = int(str(datetime.datetime.now().time()).split(":")[0])
# Needed for renaming files
import os
# List of all files in the folder
files = ['05-Evening.png', 'set.py', '07-Night.png', '01-Morning.png', '03-Afternoon.png', '06-Late-Evening.png', '08-Late-Night.png', '04-Late-Afternoon.png', '02-Late-Morning.png', 'now.png']
# Finds which wallpaper is currently set
directory = "/home/tessitura/8BitDay/"
for filename in os.listdir(directory):
files.remove(files[files.index(filename)])
current = ''.join(filename)
# Puts back the current wallpaper
path = os.path.join(directory, 'now.png')
os.rename(path, current)
# Gets out the new wallpaper based on time
if 0 <= time <= 3:
os.rename('08-Late-Night.png', 'now.png')
elif 4 <= time <= 5:
os.rename('01-Morning.png', 'now.png')
elif 6 <= time <= 10:
os.rename('02-Late-Morning.png', 'now.png')
elif 11 <= time <= 14:
os.rename('03-Afternoon.png', 'now.png')
elif 15 <= time <= 16:
os.rename('04-Late-Afternoon.png', 'now.png')
elif 17 <= time <= 18:
os.rename('06-Late-Evening.png', 'now.png')
elif 19 <= time <= 23:
os.rename('07-Night.png', 'now.png')
# Refreshes the desktop
os.system("xfdesktop --reload")
更新:Blckknght 的解决方案修复了脚本。 Mint 13 一切正常,但我已经升级到 Mint 17.1,我又遇到了问题。该脚本独立运行时运行良好,但这一次,问题出在 crontab 上。运行每小时一次的 cronjob 结果如下:
Failed to parse arguments: Cannot open display:
我把 cronjob 改成了这个...
PATH=/usr/bin/python/:/usr/bin/python3/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
@hourly DISPLAY=:0.0 /home/tessitura/8BitDay/set.py > /home/tessitura/set.log 2>&1
这给了我这个错误:
Failed to connect to session manager: Failed to connect to the session manager: SESSION_MANAGER environment variable not defined
** (xfdesktop:9739): WARNING **: xfdesktop: already running, quitting.
您当前的代码比需要的更复杂,因为您正在重命名文件,因此需要将当前的 now.png
文件重命名回其原始名称。
将适合当时的文件复制到新名称,覆盖该位置的现有文件(如果存在)会简单得多。因为您是复制而不是重命名,所以您永远不需要撤消该过程。
这是您使用 shutil.copy
执行此操作的代码版本:
import datetime
import os
import shutil
time = datetime.datetime.now().hour # no need for string parsing to get the hour
# Gets out the new wallpaper based on time
if 0 <= time <= 3:
shutil.copy('08-Late-Night.png', 'now.png')
elif 4 <= time <= 5:
shutil.copy('01-Morning.png', 'now.png')
elif 6 <= time <= 10:
shutil.copy('02-Late-Morning.png', 'now.png')
elif 11 <= time <= 14:
shutil.copy('03-Afternoon.png', 'now.png')
elif 15 <= time <= 16:
shutil.copy('04-Late-Afternoon.png', 'now.png')
elif 17 <= time <= 18:
shutil.copy('06-Late-Evening.png', 'now.png')
elif 19 <= time <= 23:
shutil.copy('07-Night.png', 'now.png')
# Refreshes the desktop
os.system("xfdesktop --reload")
我正在使用 xfce 运行 Linux Mint 13。使用 this thread 中的脚本,我能够以这种格式运行 cronjob:
PATH=/usr/bin/python/:/usr/bin/python3/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# The Wallpaper Changer
0 * * * * /home/tessitura/8BitDay/set.py
...但我遇到了脚本本身的一些问题。照原样,只改变目录名称,它给了我以下错误:
Traceback (most recent call last):
File "./set.py", line 19, in <module>
os.rename('now.png', current)
OSError: [Errno 2] No such file or directory
我试过稍微调整一下代码,它有点管用;墙纸发生变化,但 now.png 最终被删除,导致 cronjob 运行时出现空白图像。这是我现在拥有的:
#!/usr/bin/python3
# Finds the current hour
import datetime
time = int(str(datetime.datetime.now().time()).split(":")[0])
# Needed for renaming files
import os
# List of all files in the folder
files = ['05-Evening.png', 'set.py', '07-Night.png', '01-Morning.png', '03-Afternoon.png', '06-Late-Evening.png', '08-Late-Night.png', '04-Late-Afternoon.png', '02-Late-Morning.png', 'now.png']
# Finds which wallpaper is currently set
directory = "/home/tessitura/8BitDay/"
for filename in os.listdir(directory):
files.remove(files[files.index(filename)])
current = ''.join(filename)
# Puts back the current wallpaper
path = os.path.join(directory, 'now.png')
os.rename(path, current)
# Gets out the new wallpaper based on time
if 0 <= time <= 3:
os.rename('08-Late-Night.png', 'now.png')
elif 4 <= time <= 5:
os.rename('01-Morning.png', 'now.png')
elif 6 <= time <= 10:
os.rename('02-Late-Morning.png', 'now.png')
elif 11 <= time <= 14:
os.rename('03-Afternoon.png', 'now.png')
elif 15 <= time <= 16:
os.rename('04-Late-Afternoon.png', 'now.png')
elif 17 <= time <= 18:
os.rename('06-Late-Evening.png', 'now.png')
elif 19 <= time <= 23:
os.rename('07-Night.png', 'now.png')
# Refreshes the desktop
os.system("xfdesktop --reload")
更新:Blckknght 的解决方案修复了脚本。 Mint 13 一切正常,但我已经升级到 Mint 17.1,我又遇到了问题。该脚本独立运行时运行良好,但这一次,问题出在 crontab 上。运行每小时一次的 cronjob 结果如下:
Failed to parse arguments: Cannot open display:
我把 cronjob 改成了这个...
PATH=/usr/bin/python/:/usr/bin/python3/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
@hourly DISPLAY=:0.0 /home/tessitura/8BitDay/set.py > /home/tessitura/set.log 2>&1
这给了我这个错误:
Failed to connect to session manager: Failed to connect to the session manager: SESSION_MANAGER environment variable not defined
** (xfdesktop:9739): WARNING **: xfdesktop: already running, quitting.
您当前的代码比需要的更复杂,因为您正在重命名文件,因此需要将当前的 now.png
文件重命名回其原始名称。
将适合当时的文件复制到新名称,覆盖该位置的现有文件(如果存在)会简单得多。因为您是复制而不是重命名,所以您永远不需要撤消该过程。
这是您使用 shutil.copy
执行此操作的代码版本:
import datetime
import os
import shutil
time = datetime.datetime.now().hour # no need for string parsing to get the hour
# Gets out the new wallpaper based on time
if 0 <= time <= 3:
shutil.copy('08-Late-Night.png', 'now.png')
elif 4 <= time <= 5:
shutil.copy('01-Morning.png', 'now.png')
elif 6 <= time <= 10:
shutil.copy('02-Late-Morning.png', 'now.png')
elif 11 <= time <= 14:
shutil.copy('03-Afternoon.png', 'now.png')
elif 15 <= time <= 16:
shutil.copy('04-Late-Afternoon.png', 'now.png')
elif 17 <= time <= 18:
shutil.copy('06-Late-Evening.png', 'now.png')
elif 19 <= time <= 23:
shutil.copy('07-Night.png', 'now.png')
# Refreshes the desktop
os.system("xfdesktop --reload")