Skip to content

Instantly share code, notes, and snippets.

@iamaziz
Created March 8, 2015 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamaziz/e24745f804f7e42a155d to your computer and use it in GitHub Desktop.
Save iamaziz/e24745f804f7e42a155d to your computer and use it in GitHub Desktop.
Change file names in a directory and its sub-directories.
import os
for dpath, dnames, fnames in os.walk('/path/to/dir'):
for f in fnames:
os.chdir(dpath)
if f.endswith('.pdf'):
os.rename(f, f.replace(' ', '-'))
@iamaziz
Copy link
Author

iamaziz commented May 30, 2015

for example:

Before

dir
├── file number 0.txt
├── file number 1.txt
├── file number 2.txt
└── subfolder
    ├── file 1.txt
    └── file 2.txt

After

dir
├── file-number-0.txt
├── file-number-1.txt
├── file-number-2.txt
└── subfolder
    ├── file-1.txt
    └── file-2.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment