The intent is to set the attribute if and only if the file is newly *created*. 

A better approach would be to use os.path.exists() to test before opening and use that to decide whether or not to write the attribute. Is that what you're suggesting, or am I missing something about ``f.mode`` that provides information about whether or not a file has been newly created?