lnd-blog

方法一:

import sys,os
os.getcwd()#然后就可以看见结果了

方法二:

import os
os.path.dirname(os.path.realpath(\'__file__\'))#注意:添加单引号

python中的os.path.dirname(__file__)的使用

(1)当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如:

             python d:/pythonSrc/test/test.py

             那么将输出 d:/pythonSrc/test

(2)当"print os.path.dirname(__file__)"所在脚本是以相对路径被运行的, 那么将输出空目录,比如:

             python test.py

             那么将输出空字符串

python中的os.path.dirname(path)

语法:os.path.dirname(path) 
功能:去掉文件名,返回目录 
如:

print(os.path.dirname(\'W:\Python_File\juan之购物车.py\'))
#结果
#W:\Python_File
print(os.path.dirname(\'W:\Python_File\'))
#结果
#W:\

参考博客:

解决NameError: global name \'__file__\' is not defined

分类:

技术点:

相关文章:

  • 2021-08-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-31
  • 2021-10-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-19
  • 2022-01-24
  • 2021-06-10
  • 2021-05-20
  • 2022-02-06
  • 2022-12-23
相关资源
相似解决方案