Chad Vernon
  • Home
  • Reel/Resume
  • Work
  • Resources
  • About
  • Contact
Home » Resources » Python Scripting for Maya Artists » File Input and Output
sideBar

Search

Categories

  • CG
  • cvxporter
  • Maya
  • Personal

Archives

  • July 2010
  • May 2010
  • April 2010
  • March 2010
  • December 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007

Rss

  • Main Entries RSS
  • Comments RSS

File Input and Output

Python is used a lot in managing files and file systems.

>>> output = open(r'X:\data.dat', 'w')  # Open file for writing
>>> input = open('data.txt', 'r')       # Open file for reading
>>> x = input.read()                    # Read entire file
>>> x = input.read(5)                   # Read 5 bytes
>>> x = input.readline()                # Read next line
>>> L = input.readlines()               # Read entire file into a list of line strings
>>> output.write(“Some text”)           # Write text to file
>>> output.writelines(list)             # Write all strings in list L to file
>>> output.close()                      # Close manually

Here’s a more practical example of opening a maya ascii file and renaming myBadSphere to myGoodSphere.

>>> mayaFile = open(r'C:\myfile.ma', 'r')
>>> fileContents = mayaFile.readlines()
>>> mayaFile.close()
>>> for i in range(len(fileContents)):
>>>     fileContents[i] = fileContents[i].replace('myBadSphere', 'myGoodSphere')
>>> mayaFile = open(r'C:\myfile2.ma', 'w')
>>> mayaFile.writelines(fileContents)
>>> mayaFile.close()
Home » Resources » Python Scripting for Maya Artists » File Input and Output

© 2010 Chad Vernon