Home > Mobile >  Iterate through different folders
Iterate through different folders

Time:10-02

I am currently in a directory with many different folders. These folders are named with date and time "YYYY_MM_DD_HH-MM-SS-SSS".

I now need to iterate from folder to folder:

A) store each date and time from the folder naming in a continuous array.

B) go into that folder, read an .xlxs file, and store the data in an array

C) Exit the folder and go to the next folder.

Unfortunately, I only have one idea for B, something like:

 pd.read_excel(("{fname}/NAMEOFEXCELFILE.xlsx".format(fname = newpath)), skiprows = 1, header = None, usecols = [5])

Does anyone have any ideas for the "Move from Folder to Folder" part, including the time saving part?

Basically after running the code, I need an arrayA with the time and an arrayB, same length as arrayA, with the values from the excel files.

CodePudding user response:

The glob module could help you iterating over folders and subfolders https://docs.python.org/3/library/glob.html

CodePudding user response:

You can use Path.iterdir to iterate through the folders. Assuming the current working directory is the one with all the datetime folders and they are the only things there, you can loop over each folder like so:

from pathlib import Path

# ...

folder_names = []
spreadsheet_contents = []

current_directory = Path.cwd()
for folder in current_directory.iterdir():
    folder_names.append(folder.name)

    spreadsheet_path = folder / "spreadsheet.xlsx"
    spreadsheet_contents.append(pd.read_excel(spreadsheet_path, ...))

Note that the folder names will still be strings. If you want to parse the actual dates and times, you can use datetime.strptime.

  • Related