with open() fails if the path is passed as argument, but works if I passed as variable

38 views
Skip to first unread message

Rudi Hammad

unread,
Jun 9, 2023, 10:41:17 AM6/9/23
to Python Programming for Autodesk Maya
hi,
okey let me explain with an example which will be faster. It is a small code so I'll just past it here. I have the function:

def saveTreeDataToFile(filePath, treeWidget):
    treeData = json.loads(serializeTree(treeWidget))
    print(filePath) # Result: "D:/CHIMERA STUDIO/CHIMERA CGI/workspace/cfg_data.json"
   
    with open(filePath, mode="w") as f:
        json.dump(treeData, f, indent=4)

I printed the file path just to check which is "D:/CHIMERA STUDIO/CHIMERA CGI/workspace/cfg_data.json". So there is no back slash issue or anything. I get the following error:

# OSError: [Errno 22] Invalid argument: '"D:/CHIMERA STUDIO/CHIMERA CGI/workspace/cfg_data.json"'


however, if override manually filePath with the result that what was printed like so:

def saveTreeDataToFile(filePath, treeWidget):
    treeData = json.loads(serializeTree(treeWidget))
    filePath = "D:/CHIMERA STUDIO/CHIMERA CGI/workspace/cfg_data.json"
    with open(filePath, mode="w") as f:
        json.dump(treeData, f, indent=4)

...everything works fine. How is this possible? If it is a formatting issue I don't see it.
Any idea what is happening ?

thanks,
R

Marcus Ottosson

unread,
Jun 9, 2023, 10:54:22 AM6/9/23
to python_in...@googlegroups.com

Possibly unicode related, try print(repr(filePath)) and print(type(filePath)). Strings coming out of Qt is often unicode.

Second possible cause is that there are “ included in the path, notice the triple ‘ on the invalid argument warning.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/4eae1d5e-791d-4afc-bea0-489ec135b057n%40googlegroups.com.

Marcus Ottosson

unread,
Jun 9, 2023, 10:57:49 AM6/9/23
to python_in...@googlegroups.com

Also copy/pasted the question into ChatGPT out of curiosity and got a similar response.


The issue you are encountering seems to be related to the formatting of the file path string. It appears that the filePath parameter you are passing to the saveTreeDataToFile function includes double quotes (“) around the path, resulting in the error you’re experiencing.

The error message you provided, OSError: [Errno 22] Invalid argument: ‘“D:/CHIMERA STUDIO/CHIMERA CGI/workspace/cfg_data.json”‘, indicates that the path is being treated as an invalid argument due to the presence of the double quotes.

To resolve this issue, you should pass the file path without the double quotes. If you are calling the saveTreeDataToFile function with the filePath parameter in quotes like this:

saveTreeDataToFile('"D:/CHIMERA STUDIO/CHIMERA CGI/workspace/cfg_data.json"', treeWidget)

You can modify the function call to remove the quotes:

saveTreeDataToFile("D:/CHIMERA STUDIO/CHIMERA CGI/workspace/cfg_data.json", treeWidget)

By removing the double quotes, the file path should be passed correctly, and the function should work as expected.

Rudi Hammad

unread,
Jun 9, 2023, 1:46:13 PM6/9/23
to Python Programming for Autodesk Maya
ahhh , yes. That 's it.
Thanks Marcus. At some point in my code I was storing data using the path using json.dumps(thePath), when I stored directly thePath everything went okey.
Cheers
Reply all
Reply to author
Forward
0 new messages