Wednesday, May 17, 2017

Python pathlib - Move and rename documents placed in several nested folders into a new single folder


Let's say I have these files across several folders 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
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.

Using Python should be enough to complete this task. Pathlib is a module that manipulate filesytem paths as string objects. This module is best used with Python 3.2 or later, but it is also compatible with Python 2.7. If using it with Python 3.3, you also have access to optional openat-based filesystem operations.
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