如何为我目录中的每个文件运行此python函数?

2024-10-02 08:18:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个功能,可以清理列表中的csv文件:

fileListLocalDaily = (glob.glob("/path/to/my/directory/*.csv")
for y in fileListLocalDaily:
    data = y
    def prepare_file(y):
        data = y
        lines = pd.read_csv(data, sep=",", quoting=csv.QUOTE_NONE)
        new_lines = lines.replace('something', '', regex=True)
        f = StringIO(data)
        # extract date
        date = next(f)
        date = date.split('_')[1]
        date = os.path.splitext(date)[0]
        new_lines.insert(2,'date',date)
        new_lines.drop(new_lines.columns[0:2], axis=1, inplace=True)
        new_lines.drop(new_lines.columns[6], axis=1, inplace=True)
        new_lines=new_lines.sort_values(by=['Something'], ascending=False) 
        new_lines.to_csv('/path/to/my/output/'+date+'.csv', index = False)
        complete = prepare_file(data)
runFunction = prepare_file(y)

似乎上面的函数只保存了一个文件,并在一个无休止的循环中不断地覆盖它。有人能帮助我理解我如何一个接一个地对目录中的所有csv文件运行此功能吗?谢谢


Tags: 文件csvtopath功能truenewdata
2条回答

根据您提供的代码,您的循环实际上没有做任何事情。您一次又一次地定义函数,但没有在循环中调用它,因为最后一行的缩进在循环之外。你的函数在最后也调用了它自己,所以它进入了一个无限循环。您应该定义函数一次,然后在循环内部调用它:

def prepare_file(data):
    lines = pd.read_csv(data, sep=",", quoting=csv.QUOTE_NONE)
    new_lines = lines.replace('something', '', regex=True)
    f = StringIO(data)
    # extract date
    date = next(f)
    date = date.split('_')[1]
    date = os.path.splitext(date)[0]
    new_lines.insert(2,'date',date)
    new_lines.drop(new_lines.columns[0:2], axis=1, inplace=True)
    new_lines.drop(new_lines.columns[6], axis=1, inplace=True)
    new_lines=new_lines.sort_values(by=['Something'], ascending=False) 
    new_lines.to_csv('/path/to/my/output/'+date+'.csv', index = False)

fileListLocalDaily = (glob.glob("/path/to/my/directory/*.csv")
for data in fileListLocalDaily:
    prepare_file(data)

prepare_file没有返回任何内容,因此赋值操作符只是在那里赋值None,所以我删除了赋值。我还将循环和函数中的y直接重命名为data

我喜欢使用os.walk递归地获取所有文件

import os
top = '/path/to/my/directory'
for root, dirs, files in os.walk(top):
for name in files:
    if os.path.splitext(name) == ".csv":
        # do stuff with name here
        # use os.path.join(root, name) for the full file path

相关问题 更多 >

    热门问题