方法一:

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:\

参考博客:

python中的os.path.dirname与os.path.dirname(__file__)的用法

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

python3 获取当前路径及os.path.dirname的使用的更多相关文章

  1. Python os.path.dirname(__file__) os.path.join(str,str)

    Python os.path.dirname(__file__) Python os.path.join(str,str)   (1).当"print os.path.dirname(__f ...

  2. Python——os.path.dirname(__file__) 与 os.path.join(str,str)

    Python os.path.dirname(__file__) Python os.path.join(str,str)   (1).当"print os.path.dirname(__f ...

  3. os.path.join合并 os.path.dirname返回上一级目录 os.path.exists(path) os.stat('path/filename')获取文件/目录信息

    import os str1 = "grsdgfd" str2 = "wddf" str3 = "gddgs" # print(str1 + ...

  4. Python模块详解以及import本质,获得文件当前路径os.path.abspath,获得文件的父目录os.path.dirname,放到系统变量的第一位sys.path.insert(0,x)

    模块介绍 1.定义: 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test) 包:用来从逻 ...

  5. Python3基础 os.path.dirname 对路径字符串进行处理 返回所在文件夹的路径

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  6. os.path.dirname(__file__)

    os.path.dirname(__file__) 返回脚本的路径 描述: 必须实际存在的.py文件,如果直接在命令行执行,则会引发异常NameError: name 'file' is not de ...

  7. python os.path.dirname 是什么目录

    这个获取文件路径中所在的目录. 1 2 3 4 5 6 7 In [1]: import os   In [2]: os.__file__ Out[2]: '/usr/lib/python2.7/os ...

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

    在编程时,我们要获取当前文件所在的路径,以适合所有的工程,建立相对路径. python的os.path.dirname(__file__)非常好用,建议大家使用: import os FILE = o ...

  9. os.path.dirname(__file__)使用

    os.path.dirname(__file__)使用 该测试脚本所在的位置:D:\第1层\第2层\第3层\第4层\第5层\test11.py test11.py import os #该文件所在位置 ...

随机推荐

  1. 关于GeneXus封装方法Model的方法

     最近 刚从外地出差回来 工作任务不是很重 能够抽点时间记点东西 下午花了2个多钟头尝试了一下GeneXus的封装方法的功能,这里记一下便于自己以后查看.我们在许多项目中或多或少都会有着重复代码编写的 ...

  2. 第4篇创建harbor私有镜像库

        一.部署准备: 1.准备harbor软件包       在部署节点上:       2.挂载一个磁盘,专门存储harbor镜像和文件     3.进入到/etc/docker/harbor/目 ...

  3. python数组的复制问题

    1.a=[2,3,4,5] b=a 只是将b指向a,对b的操作会影响a 2.如果需要对b操作,不影响a b=a[:]

  4. git小乌龟图标不显示 windows系统

    1.修改注册表[windows + R 输入 regedit]进入注册信息,按照步骤找到 计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Curre ...

  5. Mybatis配置——自动使用驼峰命名 属性映射字段(默认为false)

    开发一个新项目,用的springboot,相关配置不太熟悉,导致一些配置没配,到具体开发时问题就暴露出来了,记录第一个配置问题----Mybatis配置-自动使用驼峰命名 属性(userId)映射字段 ...

  6. mysql 获取任意一个月的所有天数。

    SELECT ADDDATE(y.first, x.d - 1) as dFROM(SELECT 1 AS d UNION ALLSELECT 2 UNION ALLSELECT 3 UNION AL ...

  7. python--MySql(外键约束、多表查询(*****))

    两张表之间的关系: 一对一(两张表可以合并成一张表) 一对一用的比较少,多对一对外键设置unique约束可以实现一对一 一对多(例如:每一个班主任会对应多个学生 , 而每个学生只能对应一个班主任) 多 ...

  8. POJ 3414 Pots (dfs,这个代码好长啊QAQ)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  9. Python每日一题 009

    题目 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但是要分别列出来. 代码 参照网络上代码 # coding: utf-8 import os import re # ...

  10. Another Blog

    I also hold a blog with thoughts of English learning. Get there ===>. It's a private blog. Actual ...