[Tutor] reafline reads file, not just 1 line

Luke Paireepinart rabidpoobear at gmail.com
Fri Mar 30 17:48:04 CEST 2007


Kirk Bailey wrote:
> #!C:\Python25\pythonw.exe
#! is only for Unix, it's pointless here.
> #
> import os, os.path, random, sys, string, time, cgitb; cgitb.enable()
> #
> print "Content-Type: text/html\n"
> #
> try:
>     pagename=os.environ['QUERY_STRING']    # try to retreive the page  
> name requested
> except exception, e:
>     pagename="FrontPage"
> #
> if len(pagename)>3:
>     pass
> else:
>     pagename="FrontPage"            # but default to the FrontPage file.
or just:
if len(pagename < 3): pagename="FrontPage"
> #
> path=os.getcwd()
> if os.path.exists('WW.key'):
>     pass
> else:
>     f1=open('WW.key','w')
>     f1.write('unregistered')
>     f1.close()
If you find yourself passing on an if, you most likely need to take the 
complementary case.
> path=path+'\\text'
> os.chdir(path)        # make the pages dir current
> #
> """
> if os.path.exists('ConfigureMe'):
>     f1=open('ConfigureMe','r')
>     tablebgcolor=string.strip(f1.readline())
>     papercolor=string.strip(f1.readline())
>     inkcolor=string.strip(f1.readline())
>     linkcolor=string.strip(f1.readline())
>     f1.close()
This looks fine to me.  How do you know it's reading them all into the 
first item?
> else:
>     tablebgcolor='FFC0A0'
>     papercolor='White'
>     inkcolor='Black'
>     linkcolor='Blue'
>     f1=open('ConfigureMe','w')
>     f1.write(tablebgcolor+'\n')
>     f1.write(papercolor+'\n')
>     f1.write(inkcolor+'\n')
>     f1.write(linkcolor+'\n')
>     f1.write('\nThe lines above are:\n')
>     f1.write('tablecolor\n')
>     f1.write('papercolor\n')
>     f1.write('inkcolor\n')
>     f1.write('linkcolor\n\n')
What text editor are you using to view these?  Is this how you're 
determining that it's all 1 string in the first item, because there are 
no newlines in the outputted file?  I'd suggest you print out the values 
(tablebgcolor, etc) one at a time as you read them in, because it's 
possible that your text editor just chokes on \n (I believe on Windows 
you'd want to use \r\n) and that they really are stored separately.
>     f1.write('[=@! Navigation Toolbar !@=]\n')
>     f1.write('@! CategoryWiki !@\n')
>     f1.close()
> """
> tablebgcolor='FFC0A0'
> papercolor='White'
> inkcolor='Black'
> linkcolor='Blue'
>
> #
> if os.path.exists(pagename):            # if the page asked for exists,
>     pass                        # do nothing here;
> else:                            # BUT if it does NOT-
>     f1=open(pagename,'w')            # CREATE it!
>     f1.write("""Please contribute something to this webpage.
> [=@! Navigation toolbar !@=]
> @! FrontPage CategoryCategory !@""")
>     f1.close()
You really shouldn't have so many passes in if-statements.  I can't 
think of any situation where you couldn't move the else-block to the if 
and just add a 'not' to the condition.
> #
> # page header follows
> print "<html><head><title>WindowsWiki - " + pagename + "</title>"
> print '<style TYPE="text/css">'
> print "A:link, A:visited,  A:active { text-decoration:none; }"
> print "A:hover { text-decoration:underline; }"
> print 'body { margin-left: 5%; margin-right: 5%; }'
> print "</style></head>"
> print '<body bgcolor="' + papercolor + '" text="' + inkcolor + '" 
> links="' + linkcolor + '">'
> print '<table width=100% bgcolor="' + tablebgcolor + '" border="0" 
> cellpadding="10" cellspacing="0">'
> print '<tr>'
> print '<td width="120" ><B><font size="4" face="Georgia" 
> color="FF0000">WindowsWiki&trade;</font></B></td>'
> print '<td width="*" align="center" >(click title for backsearch)</td>'
> print '<td rowspan="2">&nbsp;</td>'
> print '</tr><tr>'
> print '<td>&nbsp;</td>'
> print '<td align="center"><font size="6"><a href="./MWbacksearch.py?' 
> + pagename + '">' + pagename + '</a></font></td>'
> # put a cell here if need be
> print '</tr><tr>'
> print '<td>&nbsp;</td>'
> print '<td align="center">'
I'd say you should read this in from a file.
> # Begin nagware suite
> birthday=os.stat('..\WW.key')[-1]
> now=int(time.time())
> month=60*60*24*30
> age=now-birthday
> if age > month:
>     print 'your 1 month trial period has expired.'
>     timebomb=1
> else:
>     naglist=[ 'Register and get free upgrades!','This software will 
> expire-register today!','Shareware, not freeware. Please 
> register!','Please register your shareware','Paying for shareware 
> FEELS GOOD!','Your shareware dollar rewards creativity','Do YOU get 
> paid for what you do?']
>     print random.choice(naglist)
>     timebomb=0
> # end nagware suite
You're aware that this is very easy to circumvent, right?
[snip rest of code]
Actually, my original request was for the _input file_ an an 
*attachment* not the source-code inline.
You should always include source as an attachment to prevent the e-mail 
clients from being malicious to it.


More information about the Tutor mailing list