背景重复条目蟒蛇
登录时,我写了验证我的脚本中使用的参数的函数。我的脚本正常工作,现在我正试图使用logging
库向其添加日志记录。
CODE
我已经建立了使用
import logging
logging.basicConfig(filename='AutoStats.log', level=logging.INFO, format='%(asctime)s %(message)s')
的日志我的日志记录在我的脚本工作正常使用的东西,如
logging.info('IT WORKED!!')
然而,在此记录功能:
def valid():
""" Check to see if the arguments used throughout the script are valid """
false_count = 0
for a in argv[1:]:
if a == date:
if re.match('^\d{8}$', date):
logging.info('Date is Valid')
continue
else:
logging.warning('Date parameter was invalid')
false_count += 1
if a == raw_dir or a == today_dir or a == archive_dir:
if a.endswith('\\'):
logging.info(a + 'is a valid parameter')
continue
else:
logging.warning(a + 'is not a valid paremeter, check formatting')
false_count += 1
if false_count > 0:
logging.warning('One or more of your arguments in the overruling .bat file has been entered in an incorrect format')
return False
else:
logging.info('All parameters validated')
return True
日志记录功能正常,但是重复。
不是看到
2014-09-23 13:52:42,717 Date is Valid
2014-09-23 13:52:42,717 C:\dev\OTQtxt\Correct_Ver\RAW_OTQ\is a valid parameter
2014-09-23 13:52:42,717 C:\dev\OTQtxt\Correct_Ver\today\is a valid parameter
2014-09-23 13:52:42,717 C:\dev\OTQtxt\Correct_Ver\archive\is a valid parameter
2014-09-23 13:52:42,717 All parameters validated
这是我所期待的是什么,我居然看到:
2014-09-23 13:52:42,717 Date is Valid
2014-09-23 13:52:42,717 C:\dev\OTQtxt\Correct_Ver\RAW_OTQ\is a valid parameter
2014-09-23 13:52:42,717 C:\dev\OTQtxt\Correct_Ver\today\is a valid parameter
2014-09-23 13:52:42,717 C:\dev\OTQtxt\Correct_Ver\archive\is a valid parameter
2014-09-23 13:52:42,717 All parameters validated
2014-09-23 13:52:42,717 Date is Valid
2014-09-23 13:52:42,717 C:\dev\OTQtxt\Correct_Ver\RAW_OTQ\is a valid parameter
2014-09-23 13:52:42,717 C:\dev\OTQtxt\Correct_Ver\today\is a valid parameter
2014-09-23 13:52:42,717 C:\dev\OTQtxt\Correct_Ver\archive\is a valid parameter
2014-09-23 13:52:42,717 All parameters validated
我看过了我的脚本,不能看到它遍历这个函数两次,这会导致它做到这一点。
我把logging.info
的调用放在错误的地方或错误的缩进级别吗?
编辑
这是valid()
函数是如何调用:
def main():
""" Run the defined functions above in a set order to check validation, clear the RAW directory and then concatenate the CSV files. All Functions are run in try/except blocks to catch and record errors"""
errors = 0
start = time.time()
if not valid():
raise TypeError('One or more of your arguments in the overruling .bat file has been entered in an incorrect format')
if valid():
do_stuff()
你在哪里调用'valid()'函数? – 2014-09-23 13:00:26
你可以调用'valid()'2次。将'do_stuff()'放入'else'块中。 – sk11 2014-09-23 13:00:32