2015-02-23 59 views

回答

0
import os 

def openFile(filename): 
    try: 
     fd = os.open(filename, os.O_CREAT | os.O_EXCL | os.O_WRONLY) 
    except: 
     print "Nothing here" 
     return None 
    fobj = os.fdopen(fd, "w") 
    return fobj 
1

您仍然可以使用os.path而不是「始終更改路徑」,如您在評論中所述。

import os 

def is_file_in_app_path(filename): 
    app_path = os.path.dirname(os.path.realpath(__file__)) 
    file_path = os.path.join(app_path, filename) 
    return os.path.exists(file_path) 

根據@ tdelaney的評論修正。

+0

'os.path.join'是一個獨立於平臺的版本''/'。join(...)' – tdelaney 2015-02-23 18:32:52

相關問題