PDA

View Full Version : Python File Object Problems


HueyFreeman
02-15-2008, 10:43 PM
I sat down on a Mac, and created a set of scripts to import/export transfrom data to .chan files for working with Houdini. My scripts work exactly as expected when run on a Mac, but when I ran them on an XP Machine, the scripts would complete crash Maya. After some debugging I concluded the problem was when I tried to create a File object to read/write the data out to a file. I was using the command:


f = open("/path/to/file", 'wU')
f.write(self.makeDataString(paramlist, i))


After checking through the documentation and confirming that this is what I am supposed to do in Python (There is no maya.cmds.fopen() ). I rewrote part of the script using the maya.mel.eval, this works on both the OS X and XP. However now every line I output to the file is surrounded by a single quote. This makes the data unusable for Houdini without opening the file and removing the quotes by hand. Below is the modified python code:


f = maya.mel.eval('fopen "' + filepath + '" "w"')
maya.mel.eval('fprint ' + `f` + ' "' + `self.makeDataString(paramlist, i)` + '"')


Sample Output:

'0 0 0 0 0 0
''0 0.2 0 15 0
''0 0.4 0 30 0
'


Note: the makeDataString function returns a string always ending with '\n' which is why the second quote is on a newline.

My question is, has anyone else had this problem in either Python or Mel? What did you do to fix this? Any suggestions would be greatly appreciated.

~MP

BenBarker
02-15-2008, 11:27 PM
Hmm, I've definitely successfully written files in XP. Haven't tried on a Mac. Perhaps the file mode behavior is different? I always use just 'w', I am not familiar with 'wU'.

HueyFreeman
02-16-2008, 06:16 PM
Just using 'w' seems to work. The 'wU' is suppose to mean Write Universally, it supposed to help decode '\r', '\n', and '\r\n' to help with compatibility between Mac and Unix I believe.
Thanks,
~MP

Chadrik
02-16-2008, 07:16 PM
as long as you use rU to read the files written with w mode you will avoid the newlines problem.

Chadrik
02-18-2008, 08:17 PM
>>> help(file)
Help on class file in module __builtin__:

class file(object)
| file(name[, mode[, buffering]]) -> file object
|
| Open a file. The mode can be 'r', 'w' or 'a' for reading (default),
| writing or appending. The file will be created if it doesn't exist
| when opened for writing or appending; it will be truncated when
| opened for writing. Add a 'b' to the mode for binary files.
| Add a '+' to the mode to allow simultaneous reading and writing.
| If the buffering argument is given, 0 means unbuffered, 1 means line
| buffered, and larger numbers specify the buffer size.
| Add a 'U' to mode to open the file for input with universal newline
| support. Any line ending in the input file will be seen as a '\n'
| in Python. Also, a file so opened gains the attribute 'newlines';
| the value for this attribute is one of None (no newline read yet),
| '\r', '\n', '\r\n' or a tuple containing all the newline types seen.
|
| 'U' cannot be combined with 'w' or '+' mode.
|
| Note: open() is an alias for file().
|
| Methods defined here:
...



the key part is:

'U' cannot be combined with 'w' or '+' mode.

CGTalk Moderation
02-18-2008, 08:17 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.