Loop over multiple .csv files python/pandas
Question: I have two folders that contain +50 .csv files, I want to process al those
files in my python code with pandas. At the beginning of my code I load two
different .csv files:

    
    
    Location1 = path\tasks_01.csv'          
    Location2 = path\resource_01.csv'          
    
    dftask = pd.read_csv(Location1)
    dfresource = pd.read_csv(Location2)
    

In the middle I do all kind of different operations to structure the data etc.
At the end I save both .csv files to a new .csv file:

    
    
    dftask.to_csv(path\tasks_new.csv')
    dfresource.to_csv(path\resource_new.csv')
    

Since I have two folders, one containts the task.csv files and the other the
resource.csv files how can I edit my code in such a way I can loop over all
those files? And save them under their original name?

Hope you can help me out!


Answer: Create a list of files in each folder and then zip through both of them.

    
    
    import os
    
    files_in_folder_1 = [os.path.join(path1, f) for f in os.listdir(path1) if os.path.isfile(os.path.join(path1, f))]
    
    files_in_folder_2 = [os.path.join(path2, f) for f in os.listdir(path2) if os.path.isfile(os.path.join(path2, f))]
    
    for file1, file2 in zip(files_in_folder_1, files_in_folder_2):
        with open(file1) as f1, open(file2) as f2:
            ...
    

