0
votes

new to python. Just started a rigging class that is starting to get into scripting. I found a practice for python and I am having trouble with an error.

import maya.cmds as cmds
stockPath = "C:\Users\Dryan\Desktop\table.csv"
f = open(stockPath)
data = f.read()
f.close()
print data

This is the error I get.

> # Error: line 1: IOError: file <maya console> line 4: 22 #

Again this is just a practice to get the file of number to print in the script editor. Thank you for any help.

2
Since you don't use cmds anywhere in your program, just delete the first line. - Brent Washburne
Maya does not print a stack trace of the error, so I recommend you to wrap the operations with the file in a try/except block. Anyway, it looks like the error occurs when you try to open the file, make sure it is in the correct location and the name is not misspelled. - A. Rodas
\t is a tab character. - Tim Pietzcker

2 Answers

3
votes

The likeliest problem is that you're using backslashes in your file name, so they get interpreted as control characters. The IO error is because the filename is mangled.

try

stockPath = "C:\\Users\\Dryan\\Desktop\\table.csv"  # double slashes to get single slashes in the string

or

stockPath = "C:/Users/Dryan/Desktop/table.csv"  # it's more python-y to always use right slashes.
0
votes

As joojaa said, try to avoid using backslashes when you can. I try to always convert any incoming path to a forward slashes version, and just before outputting it I normalize it using os.path.normpath.

clean_path = any_path_i_have_to_deal_with.replace("\\", "/") 
# do stuff with it 
# (concat, XML save, assign to a node attribute...)
print os.path.normpath(clean_path) # back to the OS version