Replace file name with simple python regex script (for Automator)

时间:2015-07-31 20:03:43

标签: python regex automator

I would like to create an automator folder action to rename files that I add in.

Basically, my files are named like that :

SOMETEXT-A00B00-ANOTHERTEXT.extension

To create and run an automator action that rename a file isn't a problem, but I would like to use regex (in Python if possible, 'cause i'm not familiar with apple script or batch) to rename the file like that :

A00B00.extension

Any idea ?

1 个答案:

答案 0 :(得分:0)

If that structured name stands as a rule, in Python it is easier to use split than regex for such a case.

filename = 'SOMETEXT-A00B00-ANOTHERTEXT.extension'
base = filename.split('-')[1]
extension = filename.split('.')[1]
new_filename = base + '.' + extension
print new_filename

result

A00B00.extension