Skip to content

Instantly share code, notes, and snippets.

@igniteflow
Created September 19, 2011 16:41
Show Gist options
  • Select an option

  • Save igniteflow/1226919 to your computer and use it in GitHub Desktop.

Select an option

Save igniteflow/1226919 to your computer and use it in GitHub Desktop.
Python script to rename files in directory, transforming spaces to hyphens and the chars to lowercase
import os
"""
Renames the filenames within the same directory to be Unix friendly
(1) Changes spaces to hyphens
(2) Makes lowercase (not a Unix requirement, just looks better ;)
Usage:
python rename.py
"""
path = os.getcwd()
filenames = os.listdir(path)
for filename in filenames:
os.rename(filename, filename.replace(" ", "-").lower())
@divyavittal

Copy link
Copy Markdown

How about renaming with appending zeroes like name00.txt,name01.txt and so on to all files at once?

@sspatil89

Copy link
Copy Markdown

@Ubermanusch

Copy link
Copy Markdown

The internet is a beautiful place my friends, cherish.

@BobKH

BobKH commented Jan 9, 2018

Copy link
Copy Markdown

Dear all. i want to write simple script that change entire file name in folder to test1,test2,test3 so on. actually i did it was so easy. but the thing i want is that the file name should change constantly.for instance, test1,test2,test3 and when i add example.txt file to folder the file must be changed to Test4. i got file exist error. by the way i don't want to remove file itself,only name,file content shouldn't be changed. hop i could make it clear to u.

@Hellstorm90

Copy link
Copy Markdown

What if I want to change names and not directories? 🐙

@mfdeux

mfdeux commented Feb 16, 2018

Copy link
Copy Markdown

@Hellstorm90 use os.path.isfile to check if file (or os.path.isdir to check if directory)

@Jaydaar

Jaydaar commented Mar 6, 2018

Copy link
Copy Markdown

works great exactly what i needed

@megharangaswamy

Copy link
Copy Markdown

Thanks :) works great for me.

@vishaldeshmukh007

Copy link
Copy Markdown

Thanks..! it worked for me. Though my requirement is bit different.

@metalox

metalox commented Jul 15, 2018

Copy link
Copy Markdown

Thank you for sharing. Very useful "as is" and a useful little script for learning :)

@elnazzz1

elnazzz1 commented Aug 9, 2018

Copy link
Copy Markdown

I have two cameras in the raspberry pie that every time it overwrites the name of the image, for example, image 1 and image 2,
when I run it again, it overwrites on image 1 and image 2, how can I rename previous images to avoid overwriting?

Thank you

@kiranpatil222

Copy link
Copy Markdown

Thank you so much,,, I searched so many links to rename my files in a folder with a regular expression/pattern none worked, But this.

@balter-io

Copy link
Copy Markdown

Is there any way to extend this script down the tree to rename folders and files within folders???

@josean7link

Copy link
Copy Markdown

Thank you, that's what i was looking for.

@dimicharal

Copy link
Copy Markdown

Does it read the names in ascending order?

@typopaul

typopaul commented Oct 1, 2019

Copy link
Copy Markdown

Great! It’s simple, it works. 👍 Thank you!

@rdegges

rdegges commented Oct 25, 2019

Copy link
Copy Markdown

There's a simpler way to do this using the linux rename command, if you'd like:

rename --force 's/ /-/g' *

If you run that on the CLI it'll rename all files in the current directory -- exchanging spaces for dashes.

@Lewiscowles1986

Copy link
Copy Markdown

@rdegges that does not make uppercase -> lowercase though.

@darshan4255

Copy link
Copy Markdown

how can i change only the first letter of a filename into uppercase??

@askfriends

Copy link
Copy Markdown

how about removing leading space in multiple files in a directory?
i have many files in a directory and each one start with a space. so how to batch remove that?

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