2015-01-06 3 views
2

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

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