Python  os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 的区别

os.path.abspath(__file__)返回的是.py文件的绝对路径(完整路径)
os.path.dirname(__file__)返回的是.py文件的目录

import os

base_path = os.path.dirname(os.path.abspath(__file__))
driver_path = os.path.abspath(__file__) print(base_path)
print(driver_path)

结果:

C:\Test_framework\test
C:\Test_framework\test\test.py

Python  os.system() 函数

os的system原理

system函数可以将字符串转化成命令在服务器上运行;其原理是每一条system函数执行时,其会创建一个子进程在系统上执行命令行,子进程的执行结果无法影响主进程;

上述原理会导致当需要执行多条命令行的时候可能得不到预期的结果;

import os

os.system('cd /usr/local')
os.mkdir('aaa.txt)

上述程序运行后会发现txt文件并没有创建在/usr/local文件夹下,而是在当前的目录下;

使用system执行多条命令

为了保证system执行多条命令可以成功,多条命令需要在同一个子进程中运行;

import os

os.system('cd /usr/local && mkdir aaa.txt')
# 或者
os.system('cd /usr/local ; mkdir aaa.txt')

Python os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 与 os.system() 函数的更多相关文章

  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. python中os.path.dirname(__file__) 命令行 参数没有绝对路径导致数据库找不到

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

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

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

  5. 转: Python中的os.path.dirname(__file__)

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

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

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

  7. os.path.dirname(__file__)使用、Python os.path.abspath(__file__)使用

    python中的os.path.dirname(__file__)的使用 - CSDN博客https://blog.csdn.net/u011760056/article/details/469698 ...

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

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

  9. python os.path.dirname() abspath()

    测试文件的名称 path_test.py 先确定文件目录 (my_flask3) python@ubuntu:~/Desktop/flask_news_pro$ python path_test.py ...

随机推荐

  1. 转载:C/C++关于string.h头文件和string类

    学习C语言时,用字符串的函数例如stpcpy().strcat().strcmp()等,要包含头文件string.h 学习C++后,C++有字符串的标准类string,string类也有很多方法,用s ...

  2. android include标签的使用,在RelativeLayout中使用include标签需注意!!!!!

    转:http://4265337.blog.163.com/blog/static/195375820127935731114/ include和merge标记的作用主要是为了解决layout的重用问 ...

  3. 部署OpenStack问题汇总(五)--openstack中删除虚拟主机,状态一直未deleting

    [原创文章,转载请注明出处] 一.我重启了该机器,之后想删除没有创建成功的虚拟机(没有打开cpu的vt),结果发现状态一直为deleting状态.在这个状态下创建虚拟机也失败. 二.分析:在/var/ ...

  4. Unity3D Shader落雪效果

    Shader "Custom/Snow" { Properties { _MainTex ("Base (RGB)", 2D) = "white&qu ...

  5. Docker关联使用的一些工具:Clip名字服务(转载)

    Clip名字服务 Clip(http://blog.puppeter.com/read.php?7)是一个名字服务C/S架构,它将传统的IP管理维度替换为名字服务即有意义可记忆的String.Clip ...

  6. Egret容器的鼠标默认事件

    容器的鼠标默认事件   touchEnabled touchChildren touchThrough DisplayObject false \ \ DisplayObjectContainer f ...

  7. Spring Boot 商城项目

    Spring Boot 商城项目 angularJS Demo1 <html> <head> <title>angularJS Demo1</title> ...

  8. Druid在有赞的实践

    转载一篇自己在公司博客上的文章 一.Druid介绍 Druid 是 MetaMarket 公司研发,专为海量数据集上的做高性能 OLAP (OnLine Analysis Processing)而设计 ...

  9. Java中为什么需要反射?反射要解决什么问题?

    一句话概括就是使用反射可以赋予jvm动态编译的能力,否则类的元数据信息只能用静态编译的方式实现,例如热加载,Tomcat的classloader等等都没法支持 Java中编译类型有两种: 静态编译:在 ...

  10. 0001python中特殊的for迭代zip函数

    >>> a = [1,2,3,4,5] >>> b = [9,8,7,6,5] >>> length = len(a) if len(a)< ...