简体   繁体   English

使用Python自动执行文件读取和功能应用

[英]Automating the file reading and function applying using Python

How can I automate 'csv' file reading, wherein I have to read files after every five minutes and apply some operations using Pandas. 如何自动执行“ csv”文件读取,其中我必须每五分钟读取一次文件,并使用Pandas进行一些操作。 I don't want to manually read every file and name each one of it and then apply the functions (self defined) on those files. 我不想手动读取每个文件并为每个文件命名,然后将功能(自定义)应用于这些文件。 I'm a beginner in Programming. 我是编程的初学者。 These are the functions I want to apply after reading the file. 这些是我在读取文件后要应用的功能。 Thanks in advance! 提前致谢!

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)

Use the time module to sleep and perform your tasks at regular intervals (5 minutes in this case) Provide the folder/directory path and using the os.listdir(path) you can get the list of your file names. 使用时间模块以固定的时间间隔睡眠并执行任务(在这种情况下为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM