str() and repr() question

Mark T nospam at nospam.com
Thu Apr 26 10:05:21 EDT 2007


"adima" <agibov at gmail.com> wrote in message 
news:1177575127.034458.243760 at t38g2000prd.googlegroups.com...
> 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 




More information about the Python-list mailing list