0

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.

3
  • Since you don't use cmds anywhere in your program, just delete the first line. Commented Jun 12, 2013 at 0:02
  • 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. Commented Jun 12, 2013 at 0:09
  • \t is a tab character. Commented Jun 13, 2013 at 15:25

2 Answers 2

3

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. 
Sign up to request clarification or add additional context in comments.

1 Comment

or stockPath = r"C:\Users\Dryan\Desktop\table.csv" where the r means raw string that does not escape. In any case its also a maya convention to use / instead of \ like python in general so thats the right thing to do.
0

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 

2 Comments

That's not going to help here.
Yes, because the \t is a tab in the literal OP created. But otherwise yes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.