PDA

View Full Version : writing to a file using Python?


whalerider
08-08-2009, 11:26 PM
One way I can think of is using cmdFileOutput, but that may catch any command that's being executed while the file is open, not just the results I want to write to the file.
Another alternative is calling MEL's fopen command, but I'd rather not mix MEL and Python.

Is there another, Python-only, way to write to a file?

GiantG
08-09-2009, 08:59 AM
# Append mode will add data to the end of the file.

# Write mode will overwrite the file.

# Either mode will create the file automatically if it doesn't already exist,
#

logfile = open('test.log', 'w')
logfile.write('test succeeded')
logfile.close()
print file('test.log').read()

logfile = open('test.log', 'a')
logfile.write('line 2')
logfile.close()
print file('test.log').read()

whalerider
08-09-2009, 09:54 AM
Lol, I got so carried away with Maya's Python functions that forgot about the core Python functionality ;-)

thanks.

leif3d
05-23-2010, 07:35 AM
Decided to revive this thread instead of making a new one...
I'm trying to write to a file, but I'm getting some errors. I have no idea why at this point...

from pymel.core import *
#Choose a text file
fileName = fileDialog2(fileFilter='*.txt', caption='Write to a text file')
print fileName
fileWrite = open(fileName, 'w')
fileWrite.write('hello')
fileWrite.close()
print fileWrite(fileName).read()

It doesn't seem to like fileWrite, it throws this error:
# Error: TypeError: coercing to Unicode: need string or buffer, list found #

Any ideas out there?

cbamber85
05-23-2010, 08:46 AM
Can you post the full error message?

I'm guessing that the fileDialog2 command is returning a list/Path/fileReference object, the documentation is so awful it doesn't tell you.

leif3d
05-23-2010, 03:52 PM
That's the full error message...really useful... :argh:
Well, I tried fileDialog instead of fileDialog2 and it works fine. So I guess I have to convert the output somehow?

fileDialog return:
C:/Users/Leif/Desktop/xform.txt

fileDialog2 return:
[u'C:/Users/Leif/Desktop/xform.txt']

Chadrik
05-23-2010, 04:36 PM
looks like fileName is a list and not a string. open expects a string for a path.

also, you have another bug later on in your code. you need to reopen a new file before reading from it. this line:

print fileWrite(fileName).read()

should be this:

print open(fileName, 'r').read()

leif3d
05-23-2010, 04:48 PM
Thanks Chad. :)
How would I convert a path into a string? I tried str(fileName) but it didn't like that either...

leif3d
05-23-2010, 04:58 PM
Never mind...
I just had to specify a new variable with fileName[0] and it works fine.
A little strange though that the default behavior from fileDialog would be changed.

This works perfectly:

from pymel.core import *
#Choose a text file
fileName = fileDialog2()
print fileName[0]
fileWrite = open(fileName[0], 'w')
fileWrite.write('hello World')
fileWrite.close()
print open(fileName[0], 'r').read()


Why does the forum mess with the formatting? :curious:

cbamber85
05-23-2010, 05:56 PM
A little strange though that the default behavior from fileDialog would be changed.

It's to allow multiple files/dirs to be selected at once, so I'd ask why haven't they supplied this functionality years ago...

leif3d
05-23-2010, 06:29 PM
It's to allow multiple files/dirs to be selected at once, so I'd ask why haven't they supplied this functionality years ago...

Makes sense :)

CGTalk Moderation
05-23-2010, 06:29 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.