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

Νίκος nikos.the.gr33k at gmail.com
Mon Aug 9 04:22:04 EDT 2010


On 9 Αύγ, 10:38, Peter Otten <__pete... at web.de> wrote:
> Νίκος wrote:
> > Now the code looks as follows:
> > for currdir, files, dirs in os.walk('test'):
>
> >         for f in files:
>
> >                 if f.endswith('php'):
>
> >                         # get abs path to filename
> >                         src_f = join(currdir, f)
> > I just tried to test it. I created a folder names 'test' in me 'd:\'
> > drive.
> > Then i have put to .php files inside form the original to test if it
> > would work ok for those too files before acting in the whole copy and
> > after in the original project.
>
> > so i opened a 'cli' form my Win7 and tried
>
> > D:\>convert.py
>
> > D:\>
>
> > Itsjust printed an empty line and nothign else. Why didn't even try to
> > open the folder and fiels within?
> > Syntactically it doesnt ghive me an error!
> > Somehting with os.walk() methos perhaps?
>
> If there is a folder D:\test and it does contain some PHP files (double-
> check!) the extension could be upper-case. Try
>
> if f.lower().endswith("php"): ...
>
> or
>
> php_files = fnmatch.filter(files, "*.php")
> for f in php_files: ...
>
> Peter

The extension is in in lower case. folder is there, php files is
there, i dont know why it doesnt't want to go into the d:\test to find
them.

Thast one problem.

The other one is:

i made the code simpler by specifying the filename my self.

=========================
# get abs path to filename
src_f = 'd:\\test\\index.php'

# open php src file
print ( 'reading from %s' % src_f )
f = open(src_f, 'r')
src_data = f.read()		# read contents of PHP file
f.close()
=========================

but  although ti nwo finds the fiel i egt this error in 'cli':

D:\>aconvert.py
reading from d:\test\index.php
Traceback (most recent call last):
  File "D:\aconvert.py", line 16, in <module>
    src_data = f.read()         # read contents of PHP file
  File "C:\Python32\lib\encodings\cp1253.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9f in position
321: char
acter maps to <undefined>

Somethign with the damn encodings again!!



More information about the Python-list mailing list