Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Technische Universit at M unchen Institut f ur Informatik Tobias Neckel

GRS, June 2013

Python course - Tutorial 5


For the last tutorial, we suggest to write a le renamer in Python. Sometimes, large numbers of les have to be renamed, often by replacing a certain pattern appearing in the le names. In this task, you should write a program doing that automatically. Let your program accept the following options using the optparse module: -n Only show what would be done without doing it -r Traverse directories recursively The rst two non-option arguments have to be the pattern to search for (regular expression in Python syntax) and the replacement pattern; all following ones are a list of les. First let your program print all parameters and options to test whether the optparse part works. Now print only the les (not the directories) which are provided and for which the search pattern matches. The next step is to apply the replacement to the lename (rst without really changing the les, i.e. using the -n option. Print both the original and the modied lename. Make sure that the above functionality descends recursively in all directories. Finally, move the les. To be able to use this useful tool in the future from any directory on UNIX platforms, place it in your ~/bin directory, e.g., and make sure that this directory is in your PATH.

You might also like