Renaming Multiple Files (Python and Mac Feature)

Renaming Multiple Files (Python and Mac Feature)

ยท

4 min read

Python

First of all why do we need to rename multiple files at once? The answer depends on what you intended to use it for. Example multiples images with raw file names after saving it and you wanted to easily locate or find them by having a format name + a number indicating the file number. (example: project-data1.csv , project-data2.csv)

In order to achieve that using python, we need to import the os module. The OS module in python provides functions for interacting with the operating system. OS, comes under Python's standard utility modules. This module provides a portable way of using operating system dependent functionality. The os and os.path modules include many functions to interact with the file system.

Here is the code :

import os 

path = os.chdir("path\\to\\file")

i = 131

for file in os.listdir(path):

    new_file_name = "record-{}.csv".format(i)

    os.rename(file, new_file_name)

    i = i+1

First of all is to the import os so that we can make use of its functionalities such as listdir(). After importing the the os module, we can now specify the path directory in which your messy multiple filenames are located.

Then indicating the starting value of your record. In this example I use 131 as my starting format number because I already have my record from 1 to 130. Then you can now loop each file from the directory, need not to worry about anything, you just need to add this loop block and change the new_file_name string value to what ever name format that you want. In this example, you can see that there is a curly bracket in the new_file_name variable, that means that will be the the location where my i or number will be located.

Example:

Output: 
record-131, record132, record-133, and so on..

Mac

If you are using Mac, there is also a feature in which you can rename multiple files as easy as eating chicken ๐Ÿ— (Sorry for vegan readers).

All we need to do is go the directory and highlight all files that you want to rename.

Screen Shot 2019-09-27 at 1.56.18 PM.png

After highlighting all concern files, right click and click rename # items.. and another window will appear.

Screen Shot 2019-09-27 at 2.00.05 PM.png

At the top left of the window choose the Format. Then specify your custom format in the input box. Mine was record-. After that you need to specify the starting number of your file and you can specify if it is before the file name or after. In this case, we want to specify it after the file name.

Screen Shot 2019-09-27 at 2.00.34 PM.png

๐Ÿš€ Tada ! It works like a charm.

Comment down below if you have other way of renaming multiple files in your programming language of your choice. ๐ŸŽ‰ ๐ŸŽ‰ ๐ŸŽ‰