0

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无法正常运行。特别是因为这if部分工作正常。想法?

4

3 回答 3

2

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

您需要删除 external if,因为else: pass循环结束时的 a 不会做任何事情。无论如何,迭代只会完成执行(除非在这个代码块之后有更多的循环)。

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

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,所以你需要扩展这个想法来检查其他文件。如果您添加要复制的文件,您可能还会发现另一种设置标志的方法更可扩展,但这是一般的想法。在循环中跟踪您找到的内容,并在循环后检查您是否找到了所需的一切。

于 2013-08-15T16:44:24.367 回答
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()
于 2013-08-15T16:59:45.203 回答
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

最好有一个单独的函数来生成 **required 字典,其中日期适当的文件字符串作为键

于 2013-08-15T17:29:10.013 回答