Last night, I created a small python script that iterated over a list of files in a directory and removed a hard-coded set of strings from the beginning and end of the filenames.

Today I want to add a few more features to that script

  • Ask the user for the text to replace
  • Replace the first occurrence of a character

Read my previous post to see what we’ve done thus far.

Accepting User Input

In my original script I had all of my replacement strings and the directory to read from hard-coded.

Copy to Clipboard

I want the user to be able to enter in the values for these instead so it can be applied to different sets of files.  Python has a built in method for accepting string input from the user called input. 

To modify the code above to pull the info from the user, all we have to do is replace the hard-coded strings with input calls.

Copy to Clipboard

Verifying a String is a Directory

Now that the user can enter in any value for the directory, we need to make sure that the value the user inputs is actually a directory.

Currently our script crashes if the user inputs an invalid directory name

Copy to Clipboard

Once again we go to our friend from the last post, the os module.  os.path has a method named isdir which takes in a string and returns whether or not it is a directory name.

Updating our script to use this new method, it now looks like this

Copy to Clipboard

Now if a  user enters in bad data, we print out what they did wrong and gracefully exit the program.

Copy to Clipboard

Replacing the First Instance of a Character

Finally tonight I have a couple cases where the filenames I want to edit use ‘.’ instead of spaces between each word in the filename. In my case, I want the first and second word separated by ‘ – ‘ and I want all the other ‘.’ replaced with spaces.

Before: LeadingText.Category.Long.Title.Name.TrailingText.mp4

After: Category – Long Title Name.mp4

The replace method we used to remove our leading and trailing text has an additional parameter that allows us to specify the number of occurrences we want to replace. If the parameter (n) is supplied, the method only replaces the first n occurrences. If the parameter is omitted, it replaces are occurrences.

For our case we will be using both cases. First we will replace the 1st instance of ‘.’ with ‘ – ‘ after we remove our leading and trailing text.

Copy to Clipboard

Now our filename looks like this

Category – Long.Title.Name.mp4

Getting rid of the remaining occurrences of ‘.’ is a bit tricky because we still need to keep the last one for the file extension.

StackOverflow to the rescue!

You gotta love it when you find a post with a title matching your exact question with a succinct and simple solution that would have taken me quite a bit to come up with myself.

In this answer, the poster uses a regular expression to replace all periods as long as there are additional periods in the string. Like most StackOverflow posts, just copying and pasting what they have won’t work because the .replace method doesn’t support regular expressions.

Instead we have to import an additional module named re that contains several regular expression methods. The one we want in particular is re.sub which does a replace on all matches of the regular expression.

Our final version of trim_filename now looks like this

Copy to Clipboard

Which gets us the filename we are looking for

Category – Long Title Name.mp4