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

使用Python自动化文件读取和函数应用,python,pandas,csv,input,trading,Python,Pandas,Csv,Input,Trading,如何自动读取“csv”文件,每五分钟读取一次文件,并使用Pandas应用一些操作。我不想手动读取每个文件并命名每个文件,然后在这些文件上应用函数(自定义)。我是编程的初学者。这些是我在读取文件后要应用的函数。提前谢谢 df_9May = PreprocessDataframe(df_9May) #calling the function for the 9th May DF '''Reading the new DataFrame''' df_10May = pd.read_csv(

如何自动读取“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分钟) 提供文件夹/目录路径,使用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

使用scheduler自动运行脚本,使用os.listdir提供文件位置使用循环并在定义的时间间隔内重复执行某些操作。使用或列出包含文件的目录,这样您就不必手动键入所有名称。我认为您必须清楚“自动”是什么意思。如果你的意思是:运行这个代码,等待5分钟,然后再次运行它,请说出来,我,以及其他人,可以提供一个答案。使用“自动化”,我的意思是说,我在代码中给数据帧指定的名称(比如-df_9May,df_10May,df_10_11,等等)(在上面的代码中))所有功能都可以自动执行,而无需每次读取新文件时都执行。是否有一种方法可以自动命名文件,并以有序的方式命名。(如df_3_00PM或df_3_05PM)。要添加到此代码中,请记录操作前后的时间,以便考虑所用的时间。然后打电话给sleep。谢谢@GeneByun。事实上,应该这样做。否则,程序运行所需的时间将增加到300秒,并且在读取下一个文件时会有延迟。谢谢@Surya Tej,这很有帮助。:)