Tue, Nov 26, 9:37 PM CST

Renderosity Forums / Poser Python Scripting



Welcome to the Poser Python Scripting Forum

Forum Moderators: Staff

Poser Python Scripting F.A.Q (Last Updated: 2024 Sep 18 2:50 am)

We now have a ProPack Section in the Poser FreeStuff.
Check out the new Poser Python Wish List thread. If you have an idea for a script, jot it down and maybe someone can write it. If you're looking to write a script, check out this thread for useful suggestions.

Also, check out the official Python site for interpreters, sample code, applications, cool links and debuggers. This is THE central site for Python.

You can now attach text files to your posts to pass around scripts. Just attach the script as a txt file like you would a jpg or gif. Since the forum will use a random name for the file in the link, you should give instructions on what the file name should be and where to install it. Its a good idea to usually put that info right in the script file as well.

Checkout the Renderosity MarketPlace - Your source for digital art content!



Subject: problems with readline in poser. help!


nightfir ( ) posted Tue, 21 June 2005 at 9:53 AM · edited Tue, 26 November 2024 at 6:59 PM

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:

  • If you just call readlines() on open, you don't need to close a filehandle (like f in your example)
  • You may want to remove the n character at the end of the lines you get: line[:-1]

Hope that helps, good luck!

martin


Privacy Notice

This site uses cookies to deliver the best experience. Our own cookies make user accounts and other features possible. Third-party cookies are used to display relevant ads and to analyze how Renderosity is used. By using our site, you acknowledge that you have read and understood our Terms of Service, including our Cookie Policy and our Privacy Policy.