Python Forum
splitting dirs into variables?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
splitting dirs into variables?
#1
Hey guys,

I have grouped files which meets some requirements and i have it stored, but now i don't know how to seperate them from each other.

#loop through collected in the list "for profile" files and "taxcheck" files
for x in prof_list, taxc_list:
    if country1 == country2:
        paired_country_directory = x
        test = paired_country_directory[2]
        print(test)
Output:
C:\Users\user\Desktop\Tax scripts\2020\Hungary\ICS\Work\Hungary profile inflation 2020.xls C:\Users\user\Desktop\Tax scripts\2020\Hungary\ICS\Hungary_taxcheck_20.xlsm
I wonder how can i split them into variables, so it would be equal as below:

ForProfile_file = C:\Users\user\Desktop\Tax scripts\2020\Hungary\ICS\Work\Hungary profile inflation 2020.xls
TaxCheck_file = C:\Users\user\Desktop\Tax scripts\2020\Hungary\ICS\Hungary_taxcheck_20.xlsm
Reply
#2
Can you give us what prof_list and taxc_list looks like?
Reply
#3
(Jul-15-2020, 12:19 PM)palladium Wrote: Can you give us what prof_list and taxc_list looks like?

prof_list
Output:
['C:\\Users\\user\\Desktop\\Tax scripts\\2020\\Germany\\ICS\\Work\\Germany profile new survey 2020.xls', 'C:\\Users\\user\\Desktop\\Tax scripts\\2020\\Germany\\ICS\\Work\\Germany profile old update 2020.xls', 'C:\\Users\\user\\Desktop\\Tax scripts\\2020\\Hungary\\ICS\\Work\\Hungary profile inflation 2020.xls', 'C:\\Users\\user\\Desktop\\Tax scripts\\2020\\Poland\\ICS\\Work\\Poland_Profile_20.xls']
taxc_list
Output:
['C:\\Users\\user\\Desktop\\Tax scripts\\2020\\Germany\\ICS\\Germany taxcheck 2020.xlsm', 'C:\\Users\\user\\Desktop\\Tax scripts\\2020\\Germany\\ICS\\Work\\Germany civil servant taxcheck Jan 2020.xlsx', 'C:\\Users\\user\\Desktop\\Tax scripts\\2020\\Hungary\\ICS\\Hungary_taxcheck_20.xlsm', 'C:\\Users\\user\\Desktop\\Tax scripts\\2020\\Poland\\ICS\\PolandTaxcheck20.xlsx']
Reply
#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


Forum Jump:

User Panel Messages

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