Python If/Else完成If函数但不完成Else
使用Python If/Else完成If函数但不完成Else,python,python-2.7,Python,Python 2.7,使用if/else命令时遇到一些问题,其中只有部分命令在工作。当脚本运行时,如果name==check\u file,它将完成if部分,但如果为false,它将跳过else语句并移动到下一个任务。以下是代码中无法正常运行的部分: for name in zip_file_names: copy_to = copy_this.get(name) if copy_to is not None: source_file = os.path
if/else
命令时遇到一些问题,其中只有部分命令在工作。当脚本运行时,如果name==check\u file
,它将完成if
部分,但如果为false,它将跳过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
部分工作正常。想法?如果第一个如果的计算结果为真(即,到达内部如果),那么第二个也将自动计算为真,因为它是完全相同的条件
如果
,则需要删除外部的,因为循环结束时的否则: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,所以您需要扩展这个想法来检查其他文件。如果添加要复制的文件,您可能还会找到另一种设置标志的方法,这种方法更具可扩展性,但这只是一般的想法。在循环中跟踪您找到的内容,并在循环后检查您是否找到了所需的所有内容。如果第一个的计算结果为真(即,到达内部的计算结果为真,如果),则第二个也将自动计算为真,因为它的条件完全相同
如果
,则需要删除外部的,因为循环结束时的否则: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,所以您需要扩展这个想法来检查其他文件。如果添加要复制的文件,您可能还会找到另一种设置标志的方法,这种方法更具可扩展性,但这只是一般的想法。在循环中跟踪您找到的内容,并在循环后检查您是否找到了所需的所有内容。是否要对一组文件执行一个操作,对其余文件执行另一个操作?如果可以处理所有情况,请尝试一个
(编辑:更改选择标准)
要对一组文件执行一个操作,对其余文件执行另一个操作吗?如果可以处理所有情况,请尝试一个
(编辑:更改选择标准)
如果您将代码反转,那么代码会简单得多,因此您可以从所需文件的角度来看代码,而不是从所有文件的角度来看代码。最好将文件名保留在函数体之外,这样以后就可以轻松地扩展集合
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生成**所需的字典,并将日期合适的文件字符串作为键如果将其反转,代码会简单得多,因此您可以从所需文件的角度(而不是从所有文件的角度)来查看它。最好将文件名保留在函数体之外,这样以后就可以轻松地扩展集合
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来生成**所需的字典,并将日期合适的文件字符串作为键Right。删除外部if
语句,它对代码没有任何作用。好的,这完全有道理。那么,如果没有额外的(如果
),我如何只检查该文件而不检查zip文件中的其他12个文件?它是否需要另一个变量,即使该变量实际上完全相同?我编辑了原始帖子,以便向您展示为什么我最初有额外的if。正在尝试遍历文件名并仅从解压缩的zip文件中提取我想要的名称。@Ben对于您不想提取的名称,您只想什么都不做吗?看起来你的内部else
s正在尝试重新下载zip文件-如果没有找到你要查找的文件,或者如果没有找到所有文件,你想这样做吗?在开始循环文件之前,你想为每个文件声明一个标志为false(我认为这可能是最大的问题),找到所需文件时,将相应的标志设置为true,并在循环后检查所有标志。如果它们都是真的,你就可以走了;如果一个或多个为false,则表示您丢失了一个文件,需要重新下载。对。删除外部if
语句,它对代码没有任何作用。好的,这完全有道理。那么,如果没有额外的(如果
),我如何只检查该文件而不检查zip文件中的其他12个文件?它是否需要另一个变量,即使该变量实际上完全相同?我编辑了原始帖子,以便向您展示为什么我最初有额外的if。正在尝试遍历文件名并仅从解压缩的zip文件中提取我想要的名称。@Ben对于您不想提取的名称,您只想什么都不做吗?看起来你的内部else
s正在尝试重新下载zip文件-如果没有找到你要查找的文件,或者如果没有找到所有文件,你想这样做吗?在开始循环文件之前,你想为每个文件声明一个标志为false(我认为这可能是最大的问题),找到所需文件时,将相应的标志设置为true,并在循环后检查所有标志。如果它们都是真的,你就可以走了;如果一个或多个为false,则表示您丢失了一个文件,需要重新下载。为什么要检查name==sz_check_file
两次?还有其他文件,我不使用从同一zip文件中解包的文件,如果我不再次检查它