0

I'm trying to resolve this problem:

Have a tree structure like this:

── geny16
│   ├── gen26xgeny16
│   │   ├── sustrato270
│   │   │   ├── sustrato270_data01.dat
│   │   │   ├── sustrato270_data02.dat
│   │   │   └── sustrato270_data03.dat
│   │   ├── sustrato90
│   │   │   ├── sustrato90_data01.dat
│   │   │   ├── sustrato90_data02.dat
│   │   │   └── sustrato90_data03.dat
│   │   ├── tentata0
│   │   │   ├── tentata0_data01.dat
│   │   │   ├── tentata0_data02.dat
│   │   │   └── tentata0_data03.dat
│   │   └── tenteta90
│   │   │   ├── tentata90_data01.dat
│   │   │   ├── tentata90_data02.dat
│   │   │   └── tentata90_data03.dat
│   └── gen40xgeny16
│       ├── sustrato270
│       ├── sustrato90
│       ├── tenteta0
│       └── tenteta90
└── geny9
    ├── gen16xgeny9
    │   ├── sustrato270
    ├── sustrato90
    │   ├── tenteta0
    │   └── tenteta90
    ├── gen26xgeny9
    │   ├── sustrato270
    │   ├── sustrato90
    │   ├── tenteta0
    │   └── tenteta90
    └── gen40xgen9y
        ├── sustrato270
        ├── sustrato90
        ├── tenteta0
        └── tenteta90

in each deeper folder there are some .dat files that I will manipulate and plot with matplotlib. When I use a python script inside the deeper folder, my work is done: read each data file, normalize a column, superspose and plot data; but as it's the same task for each deep folder, I would like to leave the script in the root folder and that goes through the subfolders to manipulate and plot data.

I can list the name of each element in the tree with this code:

rootDir = '.'

for dirName, subdirList, fileList in os.walk(rootDir):
    print('Directorio encontrado: %s' % subdirList)
    for fname in fileList:
        print('\t%s' % fname)

But it's only a list of names, how can I use this list to navigate and execute my script to manipulate and plot data in each deeper folders?

Thanks in advance for your comments and suggestions.

Gustavo.

Gustux
  • 91
  • 1
  • 1
  • 4

1 Answers1

0

You can use pathlib

from pathlib import Path

root_dir = '.' # __file__
d = Path(root_dir).glob('*/*/*/*.dat') #*==any folder/file

for i in d:
    print(i.name, i.parent, i.parts[-2])
    print(i.resolve())
    print(i.stem)

    # code to manipulate these files here
    # e.g read, visualise and store data

See what you get. You can the do whatever you wish with the files

Prayson W. Daniel
  • 14,191
  • 4
  • 51
  • 57