首页 > 解决方案 > For 循环中的 valor 错误,带有连接读取 xml 文件的路径

问题描述

我已经完成了一个 for 循环,他在循环结束时获得了 +1 的勇气。我的 for 循环是读取一个 xml 文件。在读取文件上的标签 CP 时,它必须进行更新。我有 4 个带有不同标签的指令。但是我的 for 循环在第一个循环之后停止工作。对不起,我的英语不好。

增量工作。我用打印检查了它。

for cmd in root.iterfind('./instructs[@name="'+functions.str_instruct_number+'"]/command'):
    print('./instructs[@name="'+functions.str_instruct_number+'"]/command')
    print("cmd >",cmd.text)
    if cmd.text == "CP":
        try:
            for file in root.iterfind('./instructs[@name="'+functions.str_instruct_number+'"]/file'):
                print("file >", file.text)
                name_file = file.text
                tags_treatement_functions.CP_tag(name_file)
        except:
            print("for instruct morte")
        finally:
            print("finally CP pour voir")
            functions.instruct_number += 1
            functions.str_instruct_number = str(functions.instruct_number)
            print("INCREMENTATION", functions.instruct_number)
            print("INCREMENTATION STR", functions.str_instruct_number)
        tags_treatement_functions.RLD_tag(config.name_script)
    print(functions.instruct_number)
    print(functions.str_instruct_number)
    print(root.iterfind('./instructs[@name="'+functions.str_instruct_number+'"]/command'))
    print('./instructs[@name="'+functions.str_instruct_number+'"]/command')
    print("for finished")
print("for stopped")

那是我的 CP_tag 功能:

def CP_tag(name_file):
    bool_copy = True
    for update_file in glob.glob(config.video_path+"*.*"):
        if name_file == update_file:
            print("pas de téléchargement")
            bool_copy = False
            break
    if bool_copy == True:
        ftp = ftplib.FTP(config.FTP_adress, timeout=5)
        ftp.login(config.FTP_user, config.FTP_pwd)
        print("download ftp")
        try:
            ftp.retrbinary('RETR ' + name_file, open(config.local_temp_path+name_file, 'wb').write)
            print("download OK")
        except:
            print("download failed")
    bool_copy = True

期望我的循环会完成 4 个循环,但它只做一个我不知道为什么,因为在一个循环之后,勇气是:INCREMENTATION 2 INCREMENTATION STR 2

谢谢您的帮助 :)

标签: pythonfor-loop

解决方案


推荐阅读