2014-09-24 37 views
0

我在Stackoverflow很新,剛剛開始學習使用Python 3.3進行編程。我只是想向您展示我的代碼,並提供以下問題。從其他跳轉到函數的開始

整個腳本將被用來將我的博客的副本從ftp服務器複製到本地硬盤。

我想檢查一個激勵名稱的文件夾是否已經存在。如果是,我想等待60秒,然後重複。 60秒,因爲在此之後,我排除了我遇到同樣的問題。

我知道你會問自己爲什麼會這傢伙讓他的博客的多個副本在一分鐘內..的一點是,我要學習如何檢查,我可以運行在這些情況下。

這是我試過,但它不工作:

def create_backup_folder(ftp, destination_directory): 
    temp = time.localtime() 
    current_datetime = "{}-{}-{}_{}-{}".format(temp.tm_year, temp.tm_mon, temp.tm_mday, temp.tm_hour, temp.tm_min) 
    if not os.path.exists(destination_directory + 'bak_' + current_datetime): 
     os.mkdir(destination_directory + 'bak_' + current_datetime, 0o777) 
     print("Backup folder successfully created!") 
    else: 
     print("Folder already exists with the current date_time_stamp. Wait 60 seconds...") 
     time.sleep(60) 
     #create_backup_folder(ftp, destination_directory) 
    newDir = destination_directory + 'bak_' + current_datetime 
    download_directory(ftp, newDir) 

的註釋掉的行給我一個錯誤:

AttributeError: 'NoneType' object has no attribute 'sendall' 

我會很感激您的答覆!非常感謝!

+0

有中'sendall'沒有打電話給你碼;也許錯誤在別處。 – nneonneo 2014-09-24 21:37:03

回答

1

當你想重試一個操作時,函數內部的for或while循環很方便。在你的情況下,你只需要進行兩次嘗試,所以for循環運行良好。我想整理一下代碼,但你最終的東西,如:

def create_backup_folder(ftp, destination_directory): 
    for i in range(2): 
     temp = time.localtime() 
     current_datetime = "{}-{}-{}_{}-{}".format(temp.tm_year, temp.tm_mon, temp.tm_mday, temp.tm_hour, temp.tm_min) 
     target_dir = destination_directory + 'bak_' + current_datetime 
     if not os.path.exists(target_dir): 
      os.mkdir(target_dir, 0777) 
      print("Backup folder successfully created!") 
      return target_dir 
     else: 
      time.sleep(60) 
    else: 
     raise Exception("Could not create backup directory in two tries") 
0

移動環路的功能之外,例如:

def create_backup_folder(ftp, destination_directory): 
    temp = time.localtime() 
    current_datetime = "{}-{}-{}_{}-{}".format(temp.tm_year, temp.tm_mon, temp.tm_mday, temp.tm_hour, temp.tm_min) 
    if not os.path.exists(destination_directory + 'bak_' + current_datetime): 
     os.mkdir(destination_directory + 'bak_' + current_datetime, 0o777) 
     print("Backup folder successfully created!") 
     return True 
    else: 
     print("Folder already exists with the current date_time_stamp. Wait 60 seconds...") 
     return False 
    newDir = destination_directory + 'bak_' + current_datetime 
    download_directory(ftp, newDir) 


done = False 
while not done: 
    done = create_backup_folder(foo, bar) 

我也建議未來與某種終止條件,因此它不會永遠運行下去。

+0

我也試過你的解決方案,但分手了,因爲我無法調用函數'download_directory(ftp,newDir)'。我嘗試了幾個職位,但總是陷入困境。不管怎樣,謝謝你! – 2014-09-24 22:12:33

0

我能夠在tdelaney的建議成功地打造成了我的腳本。

這裏是它的完整的解決方案:

(我不得不把funtion create_backup_folder的呼叫到第二環)

# Create backup folder with date and time stamp 
def create_backup_folder(ftp, destination_directory): 
    for i in range(2): 
     temp = time.localtime() 
     current_datetime = "{}-{}-{}_{}-{}".format(temp.tm_year, temp.tm_mon, temp.tm_mday, temp.tm_hour, temp.tm_min) 
     target_dir = destination_directory + 'bak_' + current_datetime 
     if not os.path.exists(target_dir): 
     os.mkdir(target_dir, mode=0o777) 
     print("Backup folder successfully created!") 
     download_directory(ftp, target_dir) 
     #return target_dir 
     else: 
      print("Please be patient") 
      time.sleep(60) 
    else: 
     raise Exception("Could not create backup directory in two tries")