I'm having a little brainfreeze here.
I want to add an index to the files and directories. I've tried so many different solutions that I finally decided to turn to the community for some pointers.
When traversing the tree directory A-1.1 is assigned index of 2 instead of 1.1, as it is the second directory os.walk enters.
I am doing this to save our secretaries at work countless hours of renaming files/directories. Any help much appreciated.
If someone wants to give it a try here's the test directory tree :
https://drive.google.com/open?id=1EoBzVPFOvixMlCdWWlUgucMd5zunI20U
As I was told download takes ages, here's zipped version :
https://drive.google.com/open?id=1I-1LKk2FRIVm9Tp2I9bYQOCOemV07jQr
The embarrassing code:
import os
rootDir = "/.../root"
rootIndex = 1
for dirName, subdirList, fileList in sorted(os.walk(rootDir)):
subindex = 1
print(f'DIR : {rootIndex} - {dirName}')
for fname in fileList:
print(f'file :: {rootIndex}.{subindex} - {fname}')
subindex = subindex + 1
### This cannot work properly as it increases the rootIndex whenever it encounters empty dir
if not subdirList:
print(f'End')
rootIndex = rootIndex + 1
The tree looks like this, with hardcoded desired result to keep track of it.
├── A-1
│ ├── A-1.1
│ │ ├── file-A-1.1.1.txt
│ │ ├── file-A-1.1.2.txt
│ │ ├── file-A-1.1.3.txt
│ │ ├── file-A-1.1.4.txt
│ │ └── file-A-1.1.5.txt
│ ├── A-1.2
│ │ ├── file-A-1.2.1.txt
│ │ ├── file-A-1.2.2.txt
│ │ ├── file-A-1.2.3.txt
│ │ ├── file-A-1.2.4.txt
│ │ └── file-A-1.2.5.txt
│ └── A-1.3
│ ├── A-1.3.1
│ │ ├── file-A-1.3.1.1.txt
│ │ ├── file-A-1.3.1.2.txt
│ │ ├── file-A-1.3.1.3.txt
│ │ ├── file-A-1.3.1.4.txt
│ │ └── file-A-1.3.1.5.txt
│ └── A-1.3.2
│ ├── file-A-1.3.2.1.txt
│ ├── file-A-1.3.2.2.txt
│ ├── file-A-1.3.2.3.txt
│ ├── file-A-1.3.2.4.txt
│ └── file-A-1.3.2.5.txt
└── B-2
├── B-2.1
│ ├── file-B-2.1.1.txt
│ ├── file-B-2.1.2.txt
│ ├── file-B-2.1.3.txt
│ ├── file-B-2.1.4.txt
│ └── file-B-2.1.5.txt
└── B-2.2
├── B-2.2.1
│ ├── file-B-2.2.1.1.txt
│ ├── file-B-2.2.1.2.txt
│ ├── file-B-2.2.1.3.txt
│ ├── file-B-2.2.1.4.txt
│ └── file-B-2.2.1.5.txt
├── B-2.2.2
│ ├── file-B-2.2.2.1.txt
│ ├── file-B-2.2.2.2.txt
│ ├── file-B-2.2.2.3.txt
│ ├── file-B-2.2.2.4.txt
│ └── file-B-2.2.2.5.txt
├── file-B-2.2.3.txt
├── file-B-2.2.4.txt
├── file-B-2.2.5.txt
├── file-B-2.2.6.txt
└── file-B-2.2.7.txt