[Tutor] raw string notation.
Israel Evans
israel@lith.com
Thu, 12 Jul 2001 12:47:06 -0700
Hi there good people!
I'm performing a regular expression search and I'm running into some
problems with the Raw String Notation.
when I type a string and I don't want any meta characters read as
metacharacters, I thought that typing r in front of the quotes. However in
this particular circumstance, this doesn't seem to work
dir = r'c:\temp\test\' #<--- this last single quote seems
# to be getting escaped by the
preceding slash
SyntaxError: invalid token #<--- hence this error.
Any ideas?
What I'm trying to do overall is to search through a directory for files,
with any one of a defined set of extensions and do something with each in
turn.
So first I made this little wordswapper module that goes like this....
#! % python wordswap
import fileinput, re
filename = raw_input('What is the name of the file? >> ')
oldword = raw_input('What is the old word? >> ')
newword = raw_input('What is the new word? >> ')
pat = re.compile(oldword)
def doswap(line):
newline = pat.sub(newword, line)
print newline,
def wordswap():
for line in fileinput.input(filename, 1):
doswap(line)
if __name__ == '__main__':
wordswap()
raw_input('press any key to end')
Then I plan to modify this so that I ask for a directory, the old word and
the new word. Then look through that Directory for any files with extensions
.txt .blah and so on.
I was trying to use glob.glob(directory + searchpattern) to do this, but I
think this is the wrong approach..
Any Ideas?
Thanks a bundle!
~Israel~