Python-如何遍历master目录,加载每个文件并直接附加它们

时间:2019-08-16 15:27:26

标签: python python-3.x

我想用特定的星期数列表遍历master目录。加载每个文件并直接附加它们。

运行此部分代码时出现错误。尝试了一些现有的解决方案,但都无济于事。

# determine what weeks to load into dataframes 


month = 1
weeks = []

for i in sched.index:
    if (month == sched.loc[i,'start_incl'].month) | (month == sched.loc[i,'end_excl'].month):
        weeks.append(sched.loc[i,'weeknum'])

# iterate through the master directory with those weeks and append to all_files.


all_files = []

spath = r'C:\\Users\\shakir\\Desktop\\FY21'
filename = (os.listdir(spath))

for f in filename.index:
    if (weeks == filename.loc[f, 'date']):
        all_files.append(filename.loc[f, 'date'])

print(all_files)

我收到此错误。

  

文件“ C:/ Users / shakir / Desktop / WIP / E-Comm Merged.py”,第35行,在    对于filename.index中的f:            TypeError:“ builtin_function_or_method”对象不可迭代

1 个答案:

答案 0 :(得分:1)

尝试使用枚举:

for f, item in enumerate(filename):
    if (weeks == filename.loc[f+1, 'date']):
        all_files.append(filename.loc[f+1, 'date'])