Replace and inserting strings within .txt files with the use of regex

MRAB python at
Tue Aug 10 00:43:17 CEST 2010

Νίκος wrote:
> D:\>
>   File "D:\", line 34
> SyntaxError: Non-ASCII character '\xce' in file D:\ on line
> 34, but no
>  encoding declared; see for
> details
> D:\>
> What does it refering too? what character cannot be identified?
> Line 34 is:
> src_data = src_data.replace( '</body>', '<br><br><center><h4><font
> color=green> Αριθμός Επισκεπτών: %(counter)d </body>' )
Didn't you say that you're using Python 2.7 now? The default file
encoding will be ASCII, but your file isn't ASCII, it contains Greek
letters. Add the encoding line:

     # -*- coding: utf-8 -*-

and check that the file is saved as UTF-8.

> Also,
> for currdir, files, dirs in os.walk('test'):
> 	for f in files:
> 		if f.lower().endswith("php"):
> in the above lines
> should i state  os.walk('test') or  os.walk('d:\test') ?

The path 'test' is relative to the current working directory. Is that
D:\ for your script? If not, then it won't find the (correct) folder.

It might be better to use an absolute path instead. You could use


(note that I've made it a raw string because it contains a backslash
which I want treated as a literal backslash) or:


(Windows should accept a slash as well as of a backslash.)

More information about the Python-list mailing list