[Tutor] Re: Epoch7: programmer newbie from hell.(formerly Re: [Tutor] Callable? What's Callable?)

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 26 Aug 2001 23:12:47 -0700 (PDT)


On Sun, 26 Aug 2001, epoch7 wrote:

> import re
> import string
> 
> file1 = raw_input("Name of file to convert: ")
> file2 = raw_input("Name of file to output to: ")
> in_file = open(file1, "r")
> out_file = open(file2, "w")
> file_text = in_file.read()
> regex = re.compile('url=(\w*)&', re.IGNORECASE)
> matches = re.findall(regex,file_text)
> out_file.write(string.join(re.findall(regex,file_text),"\n"))


It'll simplify things a little if you write the last line as:

###
out_file.write(string.join(matches, '\n'))
###

instead; otherwise, the computer isn't taking advantage of the 'matches'
variable that we made in the previous line.


> in_file.close()
> out_file.close()


Otherwise, the code looks good.  Here is an example run through your
program with the following file1.txt test file:

###
this is a small file
with an url=python&
notice, though, that it the
url=doesn't allow& punctuation.
whurl=pool&
###

###
dyoo@coffeetable:~$ python test.py
Name of file to convert: file1.txt
Name of file to output to: file1.output
dyoo@coffeetable:~$ cat file1.output
python
pool
###

So it does appear to be working.



> alright this code is looking mighty hardcore for me at the moment. and
> i thank you for all your help, but now i have to ask one more
> question: why when i run the program do i get no output? i don't get
> any errors, but now i get a 0kb txt file when it finishes. is there
> something wrong in the compile function(it is a function right?)?


If you could show us what kind of test file you're using, that might help
pinpoint the bug; perhaps the pattern used is too strict.  '\w*' will only
match up "word" characters, and that doesn't include any sort of
punctuation or spacing.  What kind of file are you planning to run your
program over?


I'm just trying to think of all the situations that might cause us to get
no result.  Hmmmm.... also, be careful that in_file isn't the same as
out_file.  Otherwise, you'll get something very unexpected:

###
pooldyoo@coffeetable:~$ python test.py
Name of file to convert: file1.txt
Name of file to output to: file1.txt
dyoo@coffeetable:~$ ls -l file1.txt
-rw-r--r--    1 dyoo     dyoo            0 Aug 26 23:05 file1.txt
###


The reason it doesn't find anything in this case is because of the
ordering of the program lines here:

> in_file = open(file1, "r")
> out_file = open(file2, "w")
> file_text = in_file.read()

which tells Python "Ok, let's open up the file1 as in_file.  Let's open up
file2 to write into it... and if there's stuff in file2 already, let's
clear it out.  Ok, now let's look at in_file's contents."  Imagine what
happens when file1 is file2, and you might see a small problem here.


If you have more questions, feel free to ask.  Good luck!