【问题标题】:i am confused with PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))我对 PROJECT_PATH = os.path.abspath(os.path.dirname(__file__)) 感到困惑
【发布时间】:2013-04-22 21:50:32
【问题描述】:

我对这个命令有疑问,任何人都可以向我描述它!

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'))

不知道能不能这样用?什么是正确的方法 谢谢你

【问题讨论】:

标签: python python-2.7


【解决方案1】:
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().

顺便说一句,你不需要在字符串中转义正斜杠,这里的连接是完全没有意义的。引用路径的正确方法是:

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'))

对垃圾邮件路径使用相同的想法。

【讨论】:

  • James Scholes 好的,我希望我能说声谢谢,但你没有提到任何对我有帮助的事情:p
  • 而且您没有提出具体问题或提及您要完成的工作。
  • 好的,我的问题是我想使用这个命令 PROJECT_PATH = os.path.abspath(os.path.dirname(file)) 但我想替换 file 变量与我的路径?
猜你喜欢
  • 2016-11-19
  • 1970-01-01
  • 2014-01-27
  • 1970-01-01
  • 2016-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-14
相关资源
最近更新 更多