Forum: Poser Python Scripting


Subject: problems with readline in poser. help!

nightfir opened this issue on Jun 21, 2005 ยท 2 posts


nightfir posted Tue, 21 June 2005 at 9:53 AM

Got a prob. I'm trying to read in data from a text file into python but I keep getting syntax errors with the readline statement. I do something like this: filename = 'c:test.txt' f = open(filename, 'r') line = readline() print line etc I'm just testing out sections of a script I'm working on I get a error involving the "r" parameter even when I remove it. Can anyone post a sample script that I can use to read info off a text file thanks


mkrueger posted Tue, 21 June 2005 at 2:00 PM

Hi,

2 things:
first:
you have to escape the backslash, since t is a tab character:
filename = 'c:test.txt'
or use a slash:
filename = 'c:/test.txt'
or use a raw string, prepending a 'r':
filename = r'c:test.txt'

second:
readline is a method which has to invoked on a file object, in your case f:
line = f.readline()

You need the 'r' as second argument in open, since this specifies that you open the file for read access.
Also, don't forget to close f with f.close() when you're finished.

the following code snippet should be all you need:

<br></br>for line in open('c:test.txt', 'r').readlines():<br></br>    print line<br></br>

2 remarks:

Hope that helps, good luck!

martin