使用 Python 自动执行文件读取和函数应用

Automating the file reading and function applying using Python

如何自动执行 'csv' 文件读取,其中我必须每五分钟读取一次文件并使用 Pandas 应用一些操作。我不想手动读取每个文件并为每个文件命名,然后在这些文件上应用函数(自定义)。我是编程初学者。这些是我阅读文件后想要应用的功能。提前致谢!

df_9May = PreprocessDataframe(df_9May) #calling the function for the 9th May DF 



'''Reading the new DataFrame''' 

df_10May = pd.read_csv('fo10MAY2018bhav.csv', parse_dates = True)
df_10May = PreprocessDataframe(df_10May)


df_9_10 = combineDFs(df_9May, df_10May)
#print("count = {}".format(count))

df_9_10 = NewNetVal_AvgPrice(df_9_10)



df_11May = pd.read_csv('fo11MAY2018bhav.csv')
df_11May = PreprocessDataframe(df_11May)


df_10_11 = combineDFs(df_9_10, df_11May)

使用时间模块定期休眠并执行任务(在本例中为 5 分钟) 提供 folder/directory 路径并使用 os.listdir(path) 您可以获得文件名列表。

import time, os
path = input("CSV Files directory path")
flist = [file for file in os.listdir(path) if file.endswith('.csv')]

while True:  # Runs indefinitely or you could assign a counter n-times
  for file in flist:
    # Read the CSV File using pandas
    # perform your custom operations
  time.sleep(300) # 300 Seconds