读取名称每天更改的csv文件

时间:2020-03-30 16:29:57

标签: python pandas csv automation filenames

我正在尝试在Pandas中读取csv文件。我正在自动化脚本以每天从csv读取数据并执行。 每天,我有csv文件的文件夹中会添加一个新的csv。新添加的csv文件具有相同的名称格式,除了其中的日期部分每天更改而月份部分每月更改。年份将每年更改。如何每天自动读取csv文件,其名称不断变化?

示例: 如果昨天的文件名为:

Name_29Mar2020_data_by_company.csv

明天的文件将是:

Name_30Mar2020_data_by_company.csv

2 个答案:

答案 0 :(得分:2)

您可以使用datetime模块:

import pandas as pd
from datetime import datetime

fname = datetime.today().strftime('Name_%d%b%Y_data_by_company.csv')
df = pd.read_csv(fname)

答案 1 :(得分:1)

这是我遇到的问题,由于文件名不一致,我最终放弃了文件名,而是创建了一个函数,用于根据最新的修改时间或创建时间获取最新文件。

from pathlib import Path

def get_latest_file(src_path,extension,method='st_mtime'):
    """ 
    Takes in a raw path and extension to parse over
    returns a single file with the last modified date

    methods:
    st_mtime: It represents the time of most recent content modification. It is 
    expressed in seconds.
    st_ctime: It represents the time of most recent metadata change on Unix 
    and creation time on Windows. It is expressed in seconds.
    """

    extension = extension if extension[0] != '.' else extension[1:]

    files = (Path(src_path).glob(f'*.{extension}'))

    if method == 'st_mtime':
        file_dictionary = {file : file.stat().st_mtime for file in files}
    elif method == 'st_ctime':
        file_dictionary = {file : file.stat().st_ctime for file in files}
    else:
        raise Exception(f'{method} not valid for this function')

    max_file = max(file_dictionary, key=file_dictionary.get)


    return max_file

latest = get_latest_file('C:/Users/DataNovice',extension='csv',method='st_mtime')

print(latest)

out : WindowsPath('C:/Users/DataNovice/new_file_i_just_created.csv')


df = pd.read_csv(latest)