Python Forum
splitting dirs into variables?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
splitting dirs into variables?
#4
If you collect first all files in a list, then you have to classify them all afterwards by country and year.
A different approach is to scan the directories, to acquire the information and then return pairwise the files.

from pathlib import Path
from pprint import pprint


def easier_grouping(root):
    root = Path(root)
    for year in root.iterdir():
        if year.is_dir() and year.name.isdecimal():
            for country in year.iterdir():
                ics_root = country / "ICS"
                xls_file = ics_root / "Work" / f"{country.name} profile inflation {year.name}.xls"
                xlsm_file = ics_root / f"{country.name}_taxcheck_{year.name[-2:]}.xlsm"
                if xls_file.exists() and xlsm_file.exists():
                    yield xls_file, xlsm_file


pprint(dict(easier_grouping("Tax scripts")), indent=4)
Output:
{ WindowsPath('Tax scripts/2020/Germany/ICS/Work/Germany profile inflation 2020.xls'): WindowsPath('Tax scripts/2020/Germany/ICS/Germany_taxcheck_20.xlsm'), WindowsPath('Tax scripts/2020/Hungary/ICS/Work/Hungary profile inflation 2020.xls'): WindowsPath('Tax scripts/2020/Hungary/ICS/Hungary_taxcheck_20.xlsm')}
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
splitting dirs into variables? - by zarize - Jul-15-2020, 11:57 AM
RE: splitting dirs into variables? - by palladium - Jul-15-2020, 12:19 PM
RE: splitting dirs into variables? - by zarize - Jul-15-2020, 12:30 PM
RE: splitting dirs into variables? - by DeaD_EyE - Jul-15-2020, 01:18 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020