从else跳转到函数的开头

时间:2014-09-24 21:25:40

标签: python python-3.x

我是Stackoverflow的新手,刚开始使用Python 3.3学习编程。我只想用以下问题向您展示我的代码。

整个脚本将用于将我的博客从ftp服务器复制到我的本地硬盘。

我想检查是否已存在具有excat名称的文件夹。如果是,我想等待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'

感谢您的回复!非常感谢!

3 个答案:

答案 0 :(得分: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")

答案 1 :(得分: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)

我还建议你提出某种终止条件,这样它就不会永远运行。

答案 2 :(得分:0)

我能够成功地将tdelaney的建议纳入我的剧本。

以下是它的完整解决方案:

(我只需要将功能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")