Предполагается, что у вас есть сценарий в том же каталоге, что и ваши файлы CSV (и все файлы CSV имеют тот же формат XXXX-MM-DD.csv
, где даты монотонно увеличиваются) и что вы сделали исполняемый файл сценария с (chmod +x script.py
) должно работать следующее script.py
:
#!/usr/bin/env python3
import os
import sys
import pandas as pd
filepath = "/path/to/csvfiles/" #[TODO] change to the path where the csv files are located.
try:
first_file = sys.argv[1]
second_file = sys.argv[2]
except:
print("An error occurred. Enter two file names separated by a space in the format of YYYY-MM-D1 YYYY-MM-D2")
sys.exit(1)
csv_files = [csv_file for csv_file in os.listdir(filepath) if csv_file.endswith('.csv')]
ordered_files = sorted(csv_files)
try:
index_first_file = ordered_files.index(first_file)
index_second_file = ordered_files.index(second_file)
except:
print("One of the csv files was not found in the current directory.")
sys.exit(1)
df_list = [pd.read_csv(ordered_files[i]) for i in range(index_first_file, index_second_file+1)]
print(df_list)
Чтобы использовать его, просто запустите, например, ./script.py 2020-03-01.csv 2020-03-02.csv