Iterate through two folders

Viewed 262

I'm still relatively new to python. I want to iterate through two folders (there are multiple files in them) and then place the output in two dataframes.

This is what I have so far

code:

import pandas
import os

def iterate(folder_path,folder_path2):
    i = 1
    os.chdir(folder_path)
    os.chdir(folder_path2)
    for (filename,filename2) in os.listdir(folder_path,folder_path2):

    df1 = pd.read_csv(filename, header=None)
    df2 = pd.read_csv(filename2, header=None)

iterate('folder_path', 'second_folder_path')

As you probably know, this does not work.

Is there something I'm missing or another way to fix it?

1 Answers

The i = 1 line suggests you are thinking of iterating, which you never actually do.

Taking the following \folder\file structure:

\one\1
\one\2
\two\3
\two\4

and leaving pandas out for the moment

from pathlib import Path

def iterate(folder_path, folder_path2):
    files1 = Path(folder_path).glob('*')
    files2 = Path(folder_path2).glob('*')
    for fhs in zip(files1, files2):
        yield fhs

for fh1, fh2 in iterate('one', 'two'):
    print(f"file1 {fh1} file2 {fh2}")

Prints

file1 one\1 file2 two\3
file1 one\2 file2 two\4

Key points are:

  • switch from os to pathlib - though either will work
  • a generator, which yields a new result each time until ...
  • zip has finished one or other of the lists
  • the lists being the directory search returned by .glob('*')

Makes no sense to me why you'd match files like this, however perhaps this is enough to help.

Related