2013-04-22 259 views
0

我有這個命令的問題任何人都可以形容它給我!我很困惑PROJECT_PATH = os.path.abspath(os.path.dirname(__ file__))

PROJECT_PATH = os.path.abspath(os.path.dirname(__file__)) 

,我想用它來指代特定的文件

PROJECT_PATH = os.path.abspath(os.path.dirname('C://Python27//the dataset//h messages' + '/ham' 

'C://Python27//the dataset//s messages ' + '/spam')) 

我不知道我是否可以這樣使用它或不?什麼是正確的方式 並感謝您

+1

看到這我的第一部分的答案:[從文本文件web2py讀取](http://stackoverflow.com/questions/14015128/14015140#14015140) – 2013-04-22 21:52:19

回答

0
The __file__ variable contains the path to the current module. The Python documentation can explain the function of os.path.abspath() and os.path.dirname(). 

BTW,你不需要逃避正斜槓字符串中,這裏串聯是完全沒有意義的。參考你的路徑正確的方法要麼是:

PROJECT_PATH = os.path.abspath(os.path.dirname('C:\\Python27\\the dataset\\h messages\\ham')) 

或:

PROJECT_PATH = os.path.abspath(os.path.dirname('C:/Python27/the dataset/h messages/ham')) 

使用同一種理念,爲您的垃圾郵件路徑。

+0

詹姆斯斯科爾斯好的我希望我能說聲謝謝但是你沒有提及任何能夠幫助我的事情:p – nana 2013-04-22 22:42:47

+0

而且你沒有提出具體的問題,也沒有提及你想要完成的任務。 – 2013-04-22 22:45:18

+0

好的我的問題是我想要使用這個命令PROJECT_PATH = os.path.abspath(os.path.dirname(__ file__)) 但我想用我的路徑替換__file__變量? – nana 2013-04-22 23:38:59