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


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)


Thanks for reply - os.path.join(…) worked great.