str() and repr() question

A

adima

Hi All!
Sorry, my English isnt good, but get a try to describe my problem.

Today we wrote next script:

import os, glob, time, string
files_to_test = ( "J:\\BWNEW\\!Unerase\\test.test", "L:\\Temp\Nick\
\test.test", "F:\\TRANSIT\\nick\\test.test")
outfile="c:\\temp\\statistic"


def DoTestTime(file):
StartTime = time.time()
filehandle = open(file)
alllines = filehandle.read()
filehandle.close()

FileSize = os.path.getsize(file)
EndTime = time.time()
return time.ctime() , "\tDisk\t" , file[:3] , "\tfile size (bytes) =
\t" , FileSize , "\taccess time =\t" , EndTime - StartTime


if __name__ == "__main__":
out = open(outfile, 'w')
for EachFile in files_to_test:
str = DoTestTime(EachFile)
print type(str)
for s in str:
print s
out.write(str(s))
out.close()

When I executed it, the output was
C:\test\py>File_Stat.py
Thu Apr 26 12:08:33 2007
<type 'str'>
Traceback (most recent call last):
File "C:\test\py\File_Stat.py", line 26, in ?
out.write(str(s))
TypeError: 'tuple' object is not callable

When I replace "str" with "repr" in out.write(str(s)), script executed
normally.

Where is the problem here?
Thanks in advance.
 
?

=?ISO-8859-15?Q?Thomas_Kr=FCger?=

adima said:
str = DoTestTime(EachFile) ^^^
print type(str)
for s in str:
print s
out.write(str(s))
^^^
Where is the problem here?
Thanks in advance.

You have overwritten the built-in str function some lines above.

Thomas
 
M

Mark T

adima said:
Hi All!
Sorry, my English isnt good, but get a try to describe my problem.

Today we wrote next script:

import os, glob, time, string
files_to_test = ( "J:\\BWNEW\\!Unerase\\test.test", "L:\\Temp\Nick\
\test.test", "F:\\TRANSIT\\nick\\test.test")
outfile="c:\\temp\\statistic"


def DoTestTime(file):
StartTime = time.time()
filehandle = open(file)
alllines = filehandle.read()
filehandle.close()

FileSize = os.path.getsize(file)
EndTime = time.time()
return time.ctime() , "\tDisk\t" , file[:3] , "\tfile size (bytes) =
\t" , FileSize , "\taccess time =\t" , EndTime - StartTime


if __name__ == "__main__":
out = open(outfile, 'w')
for EachFile in files_to_test:
str = DoTestTime(EachFile)
print type(str)
for s in str:
print s
out.write(str(s))
out.close()

When I executed it, the output was
C:\test\py>File_Stat.py
Thu Apr 26 12:08:33 2007
<type 'str'>
Traceback (most recent call last):
File "C:\test\py\File_Stat.py", line 26, in ?
out.write(str(s))
TypeError: 'tuple' object is not callable

When I replace "str" with "repr" in out.write(str(s)), script executed
normally.

Where is the problem here?
Thanks in advance.

You might want to check out pychecker.py. Its output:

Processing Script1...

Warnings...

Script1.py:1: Imported module (glob) not used
Script1.py:1: Imported module (string) not used
Script1.py:9: Local variable (alllines) not used
Script1.py:20: (str) shadows builtin

You've replaced the built-in function str with the return value of
DoTestTime().

-Mark
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top