python3 获取当前路径及os.path.dirname的使用
方法一:
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的使用的更多相关文章
- 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 ...
- 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 ...
- os.path.join合并 os.path.dirname返回上一级目录 os.path.exists(path) os.stat('path/filename')获取文件/目录信息
import os str1 = "grsdgfd" str2 = "wddf" str3 = "gddgs" # print(str1 + ...
- Python模块详解以及import本质,获得文件当前路径os.path.abspath,获得文件的父目录os.path.dirname,放到系统变量的第一位sys.path.insert(0,x)
模块介绍 1.定义: 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test) 包:用来从逻 ...
- Python3基础 os.path.dirname 对路径字符串进行处理 返回所在文件夹的路径
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- os.path.dirname(__file__)
os.path.dirname(__file__) 返回脚本的路径 描述: 必须实际存在的.py文件,如果直接在命令行执行,则会引发异常NameError: name 'file' is not de ...
- 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 ...
- python中的os.path.dirname(__file__)的使用
在编程时,我们要获取当前文件所在的路径,以适合所有的工程,建立相对路径. python的os.path.dirname(__file__)非常好用,建议大家使用: import os FILE = o ...
- os.path.dirname(__file__)使用
os.path.dirname(__file__)使用 该测试脚本所在的位置:D:\第1层\第2层\第3层\第4层\第5层\test11.py test11.py import os #该文件所在位置 ...
随机推荐
- sql datetime类型数据如果进行模糊查询
select * from Table1 where CONVERT(nvarchar(50),CreateTime,120) like '%2019'
- setlocale - 设置当前的区域选项
总览 (SYNOPSIS) #include <locale.h> char *setlocale(int category, const char * locale); 描述 (DESC ...
- 转载:Spring中各个JAR包的作用
(1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工 ...
- JS的一些日常
1. [1] == 1 => true; 很神奇.. 2.js变量命名规则: // 1.变量命名必须以字母.下划线”_”或者”$”为开头.其他字符可以是字母._.美元符号或数字. / ...
- springmvc.xml标配配置
这里提供配置模板[绝不会多余]: <context:component-scan base-package="com.atguigu"></context:com ...
- sublime-1
1.提示找不到margo go get github.com/DisposaBoy/MarGo(该工具已经被作者清空了,大部分人在这一步就被卡住了) 如果你也是在第二步卡住了,那么可以按照我的方法进行 ...
- Concurrent - 并发框架
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11426833.html SynchronizedMap和ConcurrentHashMap有什么区别? ...
- 【Shell】ps -ef 和ps aux
两者没太大差别 追溯到Unix系统中的两种风格,System V风格和BSD 风格,ps aux最初用到Unix Style中,而ps -ef被用在System V Style中,两者输出略有不同.现 ...
- [CSP-S模拟测试]:抛硬币(DP)
题目背景 小$A$和小$B$是一对好朋友,他们经常一起愉快的玩耍.最近小$B$沉迷于**师手游,天天刷本,根本无心搞学习.但是小$B$已经入坑了几个月,却一次都没有抽到$SSR$,让他非常怀疑人生.勤 ...
- 网页打开qq
网页打开qq 打开qq方法tencent://message/?uin=”+“541239271”+“&Menu=yes http://wpa.qq.com/msgrd?V=1&uin ...