[Python] Reading Specific Files from folder in Python

Discussão em 'Python' iniciado por Stack, Outubro 5, 2024.

  1. Stack

    Stack Membro Participativo

    I have folder with 12000 csv sample files and I need to read only certain files of interest from it. I have a list with filenames that I want to read from that folder. Here's my code so far

    Filenames # list that contains name of filenames that I want to read

    # Import data
    data_path= "/MyDataPath"
    data = []

    i=0
    # Import csv files
    #I feel I am doing a mistake here with looping Filenames
    for file in glob.glob(f"{data_path}/{Filenames}.csv", recursive=False):

    df = pd.read_csv(file,header=None)

    # Append dataframe
    data.append(df)
    i=i+1


    This code only reads first file and ignores all other.

    Continue reading...

Compartilhe esta Página