2013-04-22 261 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