Python If / Else Completing If功能但不是Else

时间:2013-08-15 16:42:36

标签: python python-2.7

if/else命令中遇到一些问题,其中只有部分工作正常。当脚本运行时,如果if,它将完成name == check_file部分,但如果它是假,它只是跳过else语句并转到下一个任务。以下是代码中无法正常运行的部分:

    for name in zip_file_names:
        copy_to = copy_this.get(name)
        if copy_to is not None:
            source_file = os.path.join(r'\\svr-dc\ftp site\%s\daily' % item, name)
            destination = os.path.join(r"C:\%s" % item, copy_to)
            shutil.copy(source_file, destination)
            print name, "has been verified and copied."
        elif copy_to is not None:
            print "%s has been completed." % item
        else:
            print "Repoll %s for %s" % (item, business_date_one)
            print "Once information is downloaded press any key."
            re_download = raw_input(" ")
            ext_ver_cop_one()

最后的else是针对操作不需要解压缩的文件名所以我必须传递它们,但是我不明白为什么if/else里面有if/else 1}}语句无法正常运行。特别是因为if部分工作正常。想法?

3 个答案:

答案 0 :(得分:2)

如果第一个if评估为真(即完全到达内部if),那么第二个自动也将评估为真,因为它是完全相同的条件。 / p>

您需要删除外部if,因为在循环结束时else: pass不执行任何操作。无论如何,迭代将完成执行(除非在此代码块之后循环更多)。

经过进一步讨论后,听起来你想做这样的事情:

for name in zip_file_names:
    if name == sz_check_file:
        print name, "Date is verified."
        source_file = os.path.join(r'\\svr-dc\ftp site\%s\daily' % item, sz_check_file)
        destination = os.path.join(r"C:\%s" % item, 'sales.xls')
        shutil.copy(source_file, destination)
        shutil.copy(source_file, r"C:\%s" % item)
        sz_found = true #Flag that sz was found
        print "sales.xls has been copied."
    elif name == sc_check_file:
        print name, "Date is verified."
        source_file = os.path.join(r'\\svr-dc\ftp site\%s\daily' % item, sc_check_file)
        destination = os.path.join(r"C:\%s" % item, 'cosales.xls')
        shutil.copy(source_file, destination)
        shutil.copy(source_file, r"C:\%s" % item)
        sc_found = true #Flag that sc was found
        print "cosales.xls has been copied."
#Check flags to ensure all files found
if !(sz_found&&sc_found):
    print "Repoll %s for %s" % (item, business_date_seven)
    print "Once information is downloaded press any key."
    re_download = raw_input(" ")
    ext_ver_cop_seven()

我为你的不同文件添加了标记 - 你说4,所以你需要扩展想法以检查其他文件。如果添加要复制的文件,您可能还会发现另一种设置标记的方法,这些方法更具扩展性,但这是一般的想法。跟踪你所发现的循环,并在循环后检查你是否找到了所需的一切。

答案 1 :(得分:1)

您想为一组文件执行一个操作,为其余文件执行不同的操作?如果处理所有情况,请尝试一个。

(编辑:更改选择标准)

copy_this = {sz_check_file:'sales.xls', sc_check_file:'cosales.xls'}

for name in zip_file_names:
    copy_to = copy_this.get(name)
    if copy_to is not None:
        print name, "Date is verified."
        source_file = os.path.join(r'\\svr-dc\ftp site\%s\daily' % item, name)
        destination = os.path.join(r"C:\%s" % item, copy_to)
        shutil.copy(source_file, destination)
        shutil.copy(source_file, r"C:\%s" % item)
        print name, "has been copied."
    else:
        print "Repoll %s for %s" % (item, business_date_seven)
        print "Once information is downloaded press any key."
        re_download = raw_input(" ")
        ext_ver_cop_seven()

答案 2 :(得分:0)

如果您将代码反转,则代码会更加简单,因此您可以从所需文件的角度查看代码,而不是所有文件。将文件名保留在函数体之外也更好,以便以后扩展集合很容易。

def parse_zipfile (zip_names, **required):
   '''
   found is a dictionary with all the name > filename pairs that are present
   missing is a list of the names that are not found in found
   '''
   found = dict( (item,required[item]) for item in required if item in zip_names)
   missing = [item for item in required if not item in found]
  return found, missing

required_files = {'sz_checkfile':'sales.xls', 'sc_checkfile':'cosales.xls'}

found, missing = parse_zipfile(zip_names, **required_files}
if missing:
    #print error message
else:
    for source, target in found.items():
        #copy source to target

最好有一个单独的func来生成所需的字典,并将日期相应的文件字符串作为键