Inserting-embedding some html data at the end of a .py file

Dave Angel davea at davea.name
Tue Mar 5 14:48:45 EST 2013


On 03/05/2013 01:53 PM, Νίκος Γκρ33κ wrote:
> Let's focus on just the following snipper please:

Once again, I repeat.  Make a fragment that contains enough information 
to actually run.  Explain in what environment it's running, and what you 
hoped would happen.  For example, why on earth would you assume that 
printing to the console below would write to a file instead?  Is some 
part of this running as a cgi inside a web server?  What is your 
environment?
>
> f = open( some_python_file )
> 		
> htmldata = f.read()
> counter = ''' print( "

Syntax error here.  Since the inner string is more than one line, you 
will need to use """.   See my example last message.  This only matters 
if you succeed in writing this to a python file.

> 			      <center><a href="mailto:support at superhost.gr"> <img src="/data/images/mail.png"> </a>
> 			      <center><table border=2 cellpadding=2 bgcolor=black>
> 					  <td><font color=lime>Αριθμός Επισκεπτών</td>
> 					  <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
> 			      " )
> 			  ''' % data[0]	
> 	
> #render template				
> template = htmldata + counter
> print ( template )

Why should print write to some python file you have open??  Besides you 
have the file open for readonly access.  So you might use:

f.close()
f = open( some_python_file, "w" )
f.write(template)
f.close()


> =============================
>
> What this snippet tries to accomplish is append the following string
>
> =========================================
> 		counter = ''' <center><a href="mailto:support at superhost.gr"> <img src="/data/images/mail.png"> </a>
> 			      <center><table border=2 cellpadding=2 bgcolor=black>
> 				          <td><font color=lime>Αριθμός Επισκεπτών</td>
> 					  <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
> 			  ''' % data[0
> =========================================
>
> at the end of the the python script file that it currently opened.

But that's not at all what's inside the counter variable.

> I'am using the print statemnt inside the triple quoted string so to append that html data by inserting a print statemnt but although i have changes the type of quoting it still fails.

Don't ever say "it fails".  If you can't be bothered to explain in what 
way it fails, forget it.  Perhaps you're trying to say it prints a 
string to the console instead of writing a different string to the
      /fullpath/to/some_python_file.py

Or perhaps you're saying it crashed the Windows machine, and killed 
power for miles around.  Or perhaps you're saying it got an exception 
but we won't bother telling you which.


>
> If i try to append that html data to an .html file they are inserted beautifully

Untrue, unless your environment is specified, it certainly did not. 
perhaps you were running the program with output redirected to a file, 
in which case it lost the earlier version.  Or perhaps you were running 
it on a web server, and you *think* some file changed because some 
obscure thing happened on your browser.  What's your environment?

>  but inside a .py file they dont.
>
> I just need to append that string after the end of a .py file. that's all i want to do.
>

Easy to do in emacs.


-- 
DaveA



More information about the Python-list mailing list