我试图让我的程序从外部程序读取传入的日志文件。该程序运行一个函数,然后我安排它每10秒重复一次。该程序运行良好几次迭代然后给我' ValueError:max()arg是一个空序列',我试图传递一个错误异常,但感觉我的理解是缺乏的,因此没有正确实施它。
传入的日志文件不是统一的矩阵,它们的大小可能会发生变化,因此我找到最大长度并创建一个零矩阵,用0到0填充空结果以允许操作。
CheckFile = 0
CurrentFile = 0
def MyFunction():
from datetime import datetime
global CheckFile
global CurrentFile
month = datetime.now().strftime('%B')
path = os.path.join(r'C:\MyFilePath',month) #I add month because the files change location each month
os.chdir(path)
for i in os.listdir(path):
if os.path.isfile(os.path.join(path,i)) and 'StartOfFileName$}' in i:
filelist = [i]
Filehandle = filelist[0]
strFile = str(File_3min)
CurrentFile = strFile[14:44]
Current = CurrentFile
if Current != CheckFile: # This statement checks to make sure that the log files are always new
with open(Filehandle ,'r') as Filehandle :
lines = [line.strip().split(',') for line in Filehandle ]
max_len = max(len(line) for line in lines)
my_array = np.zeros([len(lines),max_len])
for i, line in enumerate(lines):
my_array[i, :len(line)] = line
else:
return
schedule.every(10).seconds.do(MyFunction)
while 1:
schedule.run_pending()
time.sleep(1)
错误来自以下行:max_len = max(行中行的len(行))