Python-从文件夹导入日期范围内的所有文件

时间:2019-03-29 11:41:42

标签: python python-import glob

我有一个名为C:\ Flows的文件夹,在其中,今年一整天都有20190101_Flows.csv格式的多个文件。

我想每天导入一个文件,然后对其执行操作,然后保存该文件。但是,我坚持下去。

到目前为止,我已经编写了以下代码来获取文件。但是之后我被困住了。

from datetime import date

from datetime import datetime, timedelta
import glob
from os import path

DATE_FORMAT = "%Y%m%d"


pathDir = r'C:/Flows/'

dateStart = "20190301"
dateEnd = "20190305"

start_date = datetime.strptime(dateStart, DATE_FORMAT).date()
end_date = datetime.strptime(dateEnd, DATE_FORMAT).date()

delta_one_day = timedelta(days=1)

date = start_date
while date <= end_date:
    data_folder = path.join(pathDir)
    if path.isdir(data_folder):
        for filename in glob(os.path.join(data_folder, "*_Flows.csv")):

#this is where i would like to perform the operations on the file imported

           df['code']=df[df.columns[1]].astype(str).str[0:3]
            df['bmunit']=df[df.columns[1]].astype(str).str[4:]
            df['checko']=df.iloc[:,2:50].sum(axis=1)
            print filename
    date += delta_one_day

0 个答案:

没有答案