An error when i switched from python v2.6.6 => v3.2.3

  • Thread starter Íßêïò Ãêñ33ê
  • Start date
Í

Íßêïò Ãêñ33ê

Docs doesn't explain that i just take looked at http://docs.python.org/2/library/os.html#os.system

I see that the return value of os.system its an indicator to as if the command inside os.system() returned successfully.

So i guess ill be needing somethign like this:

htmltemp = '/home/nikos/www/data/private/htmltemp'

if htmlpage.endswith('.html'):
f = open( "/home/nikos/www/" + htmlpage )
htmldata = f.read()
elif htmlpage.endswith('.py'):
os.system( 'python metrites.py > %s' ) % htmltemp
f = open( htmltemp )
htmldata = f.read()

template = htmldata + counter
==============

which i just tried and now iam receiving this:

<type 'exceptions.TypeError'>: unsupported operand type(s) for %: 'int' and 'str'

I'am very close to the solution now i cna feel it but please help a bit!
 
C

Chris Angelico

os.system( 'python metrites.py > %s' ) % htmltemp

<type 'exceptions.TypeError'>: unsupported operand type(s) for %: 'int' and 'str'

Simple parenthesis problem. Take a careful look at the line mentioned
in the exception traceback (which you didn't share with us, but
fortunately the error's pretty obvious) and see if you can find the
problem.

Hint: htmltemp is the str mentioned in the error.

ChrisA
 
Í

Íßêïò Ãêñ33ê

Ôç ÐÝìðôç, 7 Ìáñôßïõ 2013 4:37:31 ì.ì. UTC+2, ï ÷ñÞóôçò Chris Angelico Ýãñáøå:
Simple parenthesis problem. Take a careful look at the line mentioned

in the exception traceback (which you didn't share with us, but

fortunately the error's pretty obvious) and see if you can find the

problem.
Hint: htmltemp is the str mentioned in the error.


/home/nikos/public_html/cgi-bin/metrites.py in ()
220 htmldata = f.read()
221 elif htmlpage.endswith('.py'):
222 os.system( 'python metrites.py > %s' ) % htmltemp
223 f = open( htmltemp )
224 htmldata = f.read()
os = <module 'os' from '/usr/lib64/python2.6/os.pyc'>, os.system = <built-in function system>, htmltemp = '/home/nikos/www/data/private/htmltemp'
<type 'exceptions.TypeError'>: unsupported operand type(s) for %: 'int' and'str'
args = ("unsupported operand type(s) for %: 'int' and 'str'",)
message = "unsupported operand type(s) for %: 'int' and 'str'"

Ôhis is the complete traceback iam reading it and reading it all over buti just dont *see* what it tries to tell me.
 
Í

Íßêïò Ãêñ33ê

Ôç ÐÝìðôç, 7 Ìáñôßïõ 2013 4:37:31 ì.ì. UTC+2, ï ÷ñÞóôçò Chris Angelico Ýãñáøå:
Simple parenthesis problem. Take a careful look at the line mentioned

in the exception traceback (which you didn't share with us, but

fortunately the error's pretty obvious) and see if you can find the

problem.
Hint: htmltemp is the str mentioned in the error.


/home/nikos/public_html/cgi-bin/metrites.py in ()
220 htmldata = f.read()
221 elif htmlpage.endswith('.py'):
222 os.system( 'python metrites.py > %s' ) % htmltemp
223 f = open( htmltemp )
224 htmldata = f.read()
os = <module 'os' from '/usr/lib64/python2.6/os.pyc'>, os.system = <built-in function system>, htmltemp = '/home/nikos/www/data/private/htmltemp'
<type 'exceptions.TypeError'>: unsupported operand type(s) for %: 'int' and'str'
args = ("unsupported operand type(s) for %: 'int' and 'str'",)
message = "unsupported operand type(s) for %: 'int' and 'str'"

Ôhis is the complete traceback iam reading it and reading it all over buti just dont *see* what it tries to tell me.
 
C

Chris Angelico

/home/nikos/public_html/cgi-bin/metrites.py in ()
220 htmldata = f.read()
221 elif htmlpage.endswith('.py'):
222 os.system( 'python metrites.py > %s' ) % htmltemp
223 f = open( htmltemp )
224 htmldata = f.read()
os = <module 'os' from '/usr/lib64/python2.6/os.pyc'>, os.system = <built-in function system>, htmltemp = '/home/nikos/www/data/private/htmltemp'
<type 'exceptions.TypeError'>: unsupported operand type(s) for %: 'int' and 'str'
args = ("unsupported operand type(s) for %: 'int' and 'str'",)
message = "unsupported operand type(s) for %: 'int' and 'str'"

Ôhis is the complete traceback iam reading it and reading it all over but i just dont *see* what it tries to tell me.

Try to get the default output from an exception, it's a lot more
useful. In any case, line 222 is the one being highlighted; you get a
couple of lines of context, but the middle one is where the error is.

ChrisA
 
Í

Íßêïò Ãêñ33ê

os.system( 'python metrites.py > htmltemp' )
f = open( htmltemp )
htmldata = f.read()

gives:

f undefined, builtin open = <built-in function open>, temp = '/home/nikos/www/data/private/htmltemp'
<type 'exceptions.IOError'>: [Errno 2] ��� ������� ������ ������ � ���������: '/home/nikos/www/data/private/htmltemp'
args = (2, '\xc4\xe5\xed \xf5\xf0\xdc\xf1\xf7\xe5\xe9 \xf4\xdd\xf4\xef\xe9\xef \xe1\xf1\xf7\xe5\xdf\xef \xde \xea\xe1\xf4\xdc\xeb\xef\xe3\xef\xf2')
errno = 2
filename = '/home/nikos/www/data/private/htmltemp'
message = ''
strerror = '\xc4\xe5\xed \xf5\xf0\xdc\xf1\xf7\xe5\xe9 \xf4\xdd\xf4\xef\xe9\xef \xe1\xf1\xf7\xe5\xdf\xef \xde \xea\xe1\xf4\xdc\xeb\xef\xe3\xef\xf2'

Very weird!
 
Í

Íßêïò Ãêñ33ê

Why f gives back an error and doesnt open the filename that i give it as anvariable successfully?
 
Í

Íßêïò Ãêñ33ê

Ôç ÐÝìðôç, 7 Ìáñôßïõ 2013 4:50:34 ì.ì. UTC+2, ï ÷ñÞóôçò Íßêïò Ãêñ33ê Ýãñáøå:
os.system( 'python metrites.py > htmltemp' )

That is wrong now i realize, it takes htmltemp as the filename where what ineed is to take it htmltemp's varibales value.

Switching back to:

os.system( 'python metrites.py > %s' ) % htmltemp
f = open( htmltemp )
htmldata = f.read()

but still donse see what iam doing wrong.....
 
C

Chris Angelico

os.system( 'python metrites.py > %s' ) % htmltemp

Manually step through what this line should do. Follow the exact same
rules Python will follow in evaluating this expression.

http://docs.python.org/2/reference/expressions.html#operator-precedence
http://docs.python.org/3/reference/expressions.html#operator-precedence

As you'll see from footnote 8 or 5 (depending on which version of the
docs), your percent operator is the same one listed in the table as a
division operator.

Now. Go through that expression, step by step. Walk through everything
the Python interpreter does. Figure out exactly what happens first,
second, third. Figure out when your percent operator gets handled.
Then you'll know what's wrong with that line of code.

You'll also gain a very useful understanding of Python, and more
generally of the way most high level languages parse expressions.

ChrisA
 
Í

Íßêïò Ãêñ33ê

Ôç ÐÝìðôç, 7 Ìáñôßïõ 2013 5:13:24 ì.ì. UTC+2, ï ÷ñÞóôçò Chris Angelico Ýãñáøå:
Manually step through what this line should do. Follow the exact same

rules Python will follow in evaluating this expression.



http://docs.python.org/2/reference/expressions.html#operator-precedence

http://docs.python.org/3/reference/expressions.html#operator-precedence



As you'll see from footnote 8 or 5 (depending on which version of the

docs), your percent operator is the same one listed in the table as a

division operator.



Now. Go through that expression, step by step. Walk through everything

the Python interpreter does. Figure out exactly what happens first,

second, third. Figure out when your percent operator gets handled.

Then you'll know what's wrong with that line of code.



You'll also gain a very useful understanding of Python, and more

generally of the way most high level languages parse expressions.



ChrisA

I'am sorry to say i cant figure this out :(
please enlight me.
 
Í

Íßêïò Ãêñ33ê

Ôç ÐÝìðôç, 7 Ìáñôßïõ 2013 5:13:24 ì.ì. UTC+2, ï ÷ñÞóôçò Chris Angelico Ýãñáøå:
Manually step through what this line should do. Follow the exact same

rules Python will follow in evaluating this expression.



http://docs.python.org/2/reference/expressions.html#operator-precedence

http://docs.python.org/3/reference/expressions.html#operator-precedence



As you'll see from footnote 8 or 5 (depending on which version of the

docs), your percent operator is the same one listed in the table as a

division operator.



Now. Go through that expression, step by step. Walk through everything

the Python interpreter does. Figure out exactly what happens first,

second, third. Figure out when your percent operator gets handled.

Then you'll know what's wrong with that line of code.



You'll also gain a very useful understanding of Python, and more

generally of the way most high level languages parse expressions.



ChrisA

I'am sorry to say i cant figure this out :(
please enlight me.
 
C

Chris Angelico

Ôç ÐÝìðôç, 7 Ìáñôßïõ 2013 5:13:24 ì.ì. UTC+2, ï ÷ñÞóôçò Chris Angelico Ýãñáøå:

I'am sorry to say i cant figure this out :(
please enlight me.

You can figure it out, but it will take some effort. I will not
enlighten you further. The documentation is all there; the answers are
available.

ChrisA
 
J

John Gordon

In said:
Switching back to:
os.system( 'python metrites.py > %s' ) % htmltemp
f =3D open( htmltemp )
htmldata =3D f.read()=09
but still donse see what iam doing wrong.....

You have the close-parentheses in the wrong place. The line should be:

os.system( 'python metrites.py > %s' % htmltemp )
 
Í

Íßêïò Ãêñ33ê

Ôç ÐÝìðôç, 7 Ìáñôßïõ 2013 5:33:10 ì.ì. UTC+2, ï ÷ñÞóôçò Chris Angelico Ýãñáøå:
You can figure it out, but it will take some effort. I will not

enlighten you further. The documentation is all there; the answers are

available.

I found it! I have placed 'htmltemp' within the os.system() call. Otherwise, os.system() returns a return value (int) which python then tries to concatenate with htmltemp (string).

os.system( 'python metrites.py > %s' % htmltemp )

:)
 
Í

Íßêïò Ãêñ33ê

Ôç ÐÝìðôç, 7 Ìáñôßïõ 2013 5:33:10 ì.ì. UTC+2, ï ÷ñÞóôçò Chris Angelico Ýãñáøå:
You can figure it out, but it will take some effort. I will not

enlighten you further. The documentation is all there; the answers are

available.

I found it! I have placed 'htmltemp' within the os.system() call. Otherwise, os.system() returns a return value (int) which python then tries to concatenate with htmltemp (string).

os.system( 'python metrites.py > %s' % htmltemp )

:)
 
Í

Íßêïò Ãêñ33ê

Ôç ÐÝìðôç, 7 Ìáñôßïõ 2013 5:55:58 ì.ì. UTC+2, ï ÷ñÞóôçò John Gordon Ýãñáøå:
You have the close-parentheses in the wrong place. The line should be:



os.system( 'python metrites.py > %s' % htmltemp )



--

John Gordon A is for Amy, who fell down the stairs

(e-mail address removed) B is for Basil, assaulted by bears

-- Edward Gorey, "The Gashlycrumb Tinies"

Yes thank you, i found it myself and moment later i also saw your post!
I'am very excites i discoverd it myself!

Now instead of receiving proper html output i receive this:

Go to http://superhost.gr please and click the 1st image you see on the top..
Instead for the pelatologio.py html output to appear + counter string

i see weird formatting, if you want please take a look.
 
Í

Íßêïò Ãêñ33ê

I have fixed this!
Still ahve allok and you will see that in both of my images they appear in the firstpage of superhost.gr

the output of .py files appear fine except of that ehen a visitor click on my first 2 image links he also sees as first line this:

print ( "Content-type: text/html; charset=utf-8\n" )

this is because the above code is a header and its contained to every cgi .py file that i generate html ouput from via os.system()

Can this be somehow eliminated?
 
J

Joel Goldstick

I have fixed this!
Still ahve allok and you will see that in both of my images they appear in
the firstpage of superhost.gr

the output of .py files appear fine except of that ehen a visitor click on
my first 2 image links he also sees as first line this:

print ( "Content-type: text/html; charset=utf-8\n" )

this is because the above code is a header and its contained to every cgi
.py file that i generate html ouput from via os.system()

Can this be somehow eliminated?

That page is not valid html. View the source in your browser to see -- it
 
J

Joel Goldstick

This may have to do with the configuration of your server. There may be
differences in how to do that for python 2.x and python 3.x. I can't help
you more than that
 
R

rh

I found it! I have placed 'htmltemp' within the os.system() call.
Otherwise, os.system() returns a return value (int) which python then
tries to concatenate with htmltemp (string).

os.system( 'python metrites.py > %s' % htmltemp )

Can also do
sterrs = 'errorsout'
os.system( 'python metrites.py > {0} 2> {1}'.format(htmltemp,sterrs))

It's more 3ish, or 3ist or 3like.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top