Forum Moderators: Staff
Poser Python Scripting F.A.Q (Last Updated: 2024 Sep 18 2:50 am)
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
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.
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