IOError [Errno 2] No such file or directory

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Display Modes
  05 May 2018
IOError [Errno 2] No such file or directory

When reading a data file in Python, it would be really handy (for portability) to just use a local address (as opposed to the full path from C:\) to locate the data file i.e. have the data file in the same folder as the scene file and just reference it by filename.

I've experimented with this, and the weird thing is, local address works - but only intermittently. I can make a test scene with a locally addressed data file that works OK. Restart C4D and open scene, and I get 'no such file or directory'. I'm sure I'm missing something obvious.

def main():
    f = open('testfile.txt', 'r')
    info =
    print info
  05 May 2018
I recommend to create an absolute path dynamically (by using c4d.documents.BaseDocument.GetDocumentPath) insteading of using a pure relative path. Relative could be misleading since it could be the scene, or the current working directory (for Python) , etc...

import os

def doc_path_join(doc, *filenames):
    Creates an absolute path given by a document and a filename. 
    The document must have a path, otherwise ValueError is raised.

        doc_path_join(doc, "test.txt")  # /example/path/test.txt
        doc_path_join(doc, "..", "test.txt")  # /example/test.txt
        doc_path_join(doc, "tex", "test.txt")  # /example/path/tex/test.txt

    @param doc       Instance of c4d.documents.BaseDocument. Must have a document path.
    @param filenames   Strings as filenames.
    directory = doc.GetDocumentPath()
    if not directory:
        raise ValueError("document has no path")
    return os.path.join(directory, *filenames)

Last edited by donelgreeko : 05 May 2018 at 07:21 PM.
  05 May 2018
Thanks for reply - os.path.join(...) worked great.
reply share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright ©2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 09:15 AM.

Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.