2015-01-06 31 views
2

我正在用xfce運行Linux Mint 13。在this thread使用腳本,我能得到這個格式運行的cronjob:根據一天中的時間更改壁紙的Python腳本

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") 

UPDATE:Blckknght的解決方案修復腳本。一切都很好,在薄荷13,但我已經升級到薄荷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. 
+0

'datetime.datetime.now()時間()hour'真正能拿到小時 –

+0

'os.listdir(路徑) '會在'path'中列出文件。這只是如果你想列出所有的文件,所以你不必硬編碼名稱 – Paco

+0

這將是簡單得多pickle或json目前正在使用的文件,只是檢查每次 –

回答

2

您當前的代碼更復雜因爲您正在重命名文件,因此需要將當前的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") 
+0

這完美的作品!謝謝! – Gizmaton