I’m new to python and I keep getting an error doing the simpliest thing.
I’m trying to use a variable in a regular expression and replace that with an *
the following gets me the error “TypeError: not all arguments converted during string formatting” and I can’t tell why. this should be so simple.
import re
file = "my123filename.zip"
pattern = "123"
re.sub(r'%s', "*", file) % pattern
Error:
Traceback (most recent call last):
File “”, line 1, in ?
TypeError: not all arguments converted during string formatting
Any tips?
You’re problem is on this line:
What you’re doing is replacing every occurance of
%swith*in the text from the stringfile(in this case, I’d recommend renaming the variablefilenameto avoid shadowing the builtinfileobject and to make it more explicit what you’re working with). Then you’re trying to replace the%sin the (already replaced) text withpattern. However,filedoesn’t have any format modifiers in it which leads to theTypeErroryou see. It’s basically the same as:which will give you the same error.
What you probably want is something more like:
which is exactly equivalent to: