dir
├── 0
│ ├── 103425.xml
│ ├── 105340.xml
│ ├── 109454.xml
│
│── 1247
│ └── doc.xml
├── 14568
│ └── doc.xml
├── 1659
│ └── doc.xml
├── 10450
│ └── doc.xml
├── 10351
│ └── doc.xml
but my goal is to place all files like this:dir
├── 0
│ ├── 103425.xml
│ ├── 105340.xml
│ ├── 109454.xml
│
│── 1247
│ └── doc.xml
├── 14568
│ └── doc.xml
├── 1659
│ └── doc.xml
├── 10450
│ └── doc.xml
├── 10351
│ └── doc.xml
So basically I want to Move and rename documents placed in several nested folders into a new single folder.
This Python code will do the job:
import os
import pathlib
OLD_DIR = 'files'
NEW_DIR = 'new_dir'
p = pathlib.Path(OLD_DIR)
for f in p.glob('**/*.xml'):
new_name = '{}_{}'.format(f.parent.name, f.name)
f.rename(os.path.join(NEW_DIR, new_name))
Programming thought of the day:
- If Bill Gates had a penny for every time I had to reboot my computer ...oh wait, he does.
No comments:
Post a Comment