官方文档看这里 https://docs.python.org/3.5/library/os.html

http://www.cnblogs.com/wupeiqi/articles/5501365.html

os.path.exists(file) 如果file存在于当前目录下,返回True,否则返回False

os.path.abspath(file) 返回file的绝对路径

os.path.dirname(file) 返回file的上级目录名

sys.path.append(path) 添加path到环境变量

os.system(command)

def system(*args, **kwargs): # real signature unknown
""" Execute the command in a subshell. """
pass

Execute the command (a string) in a subshell. 系统命令如果本身就会打印结果,那么你会在屏幕上看到结果。返回值是进程的退出状态,若成功执行,则返回值为0,若有报错,返回值为错误代码。

os.popen(command[, mode[, bufsize]])

# Supply os.popen()
def popen(cmd, mode="r", buffering=-1):
if not isinstance(cmd, str):
raise TypeError("invalid cmd type (%s, expected string)" % type(cmd))
if mode not in ("r", "w"):
raise ValueError("invalid mode %r" % mode)
if buffering == 0 or buffering is None:
raise ValueError("popen() does not support unbuffered streams")
import subprocess, io
if mode == "r":
proc = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
bufsize=buffering)
return _wrap_close(io.TextIOWrapper(proc.stdout), proc)
else:
proc = subprocess.Popen(cmd,
shell=True,
stdin=subprocess.PIPE,
bufsize=buffering)
return _wrap_close(io.TextIOWrapper(proc.stdin), proc)

执行系统命令,执行结果写到一个临时文件里面,返回值是这个打开的文件对象。mode默认值为r,即默认以只读方式打开文件;buffersize默认是系统缓冲区大小(buffer缓冲,此概念适用于磁盘写数据;cache缓存,此概念适用于磁盘读数据)。

既然返回的是一个文件对象,那么接下来可以理解os.popen().read(),是把这个文件对象中的内容读出来,返回值就是文件中的内容。

os.path.exists('文件名')

判断文件是否存在,存在返回True,不存在返回False

def mkdir(*args, **kwargs): # real signature unknown
"""
Create a directory. If dir_fd is not None, it should be a file descriptor open to a directory,
and path should be relative; path will then be relative to that directory.
dir_fd may not be implemented on your platform.
If it is unavailable, using it will raise a NotImplementedError. The mode argument is ignored on Windows.
"""
pass

python os模块sys模块常用方法的更多相关文章

  1. python os和sys模块使用

    python os和sys模块使用 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相 ...

  2. Python os与sys模块解析

    os与sys模块的官方解释如下: os: This module provides a portable way of using operating system dependent functio ...

  3. python os,sys模块的使用

    首先,os模块是用来与操作系统进行交互的模块,可以对操作系统上的一些东西进行操作 而sys是用来对解释器进行一些操作的 一.os os.getcwd() 获取当前工作目录,即当前python脚本工作的 ...

  4. Python中os与sys模块的区别

    os与sys模块的官方解释如下: os: This module provides a portable way of using operating system dependent functio ...

  5. python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则

    python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib  subprocess ...

  6. Python 的 os 与 sys 模块

    os与sys模块的官方解释如下: os: This module provides a portable way of using operating system dependent functio ...

  7. python之os和sys模块的区别

    一.os模块 os模块是Python标准库中提供的与操作系统交互的模块,提供了访问操作系统底层的接口,里面有很多操作系统的函数 1.os常用方法 import os # print(os.getcwd ...

  8. Python中os和sys模块中常用的方法

    os模块 os模块:该模块提供了一些方便使用操作系统相关功能的函数 os.remove() 删除文件 os.rename() 重命名文件 os.walk() 文件目录遍历器 os.chdir() 改变 ...

  9. Python基础-os和sys模块

    os模块提供对操作系统进行调用的接口 import os os.getcwd() # 获取当前工作目录 os.chdir(r'd:\fansik') # 修改对当前工作目录 print(os.curd ...

  10. Python进阶(九)----json模块, pickle模块, os模块,sys模块,hashlib模块

    Python进阶----json模块, pickle模块, os模块,sys模块,hashlib模块 一丶序列化模块 什么是序列化: ​ 将一种数据结构,转换成一个特殊的序列(特殊字符串,用于网络传输 ...

随机推荐

  1. PLSQL_数据泵导入进度查看Impdp/Expdp Status(案例)

    20150701 Created By BaoXinjian

  2. svn 中 版本回退

    譬如有个文件,有十个版本,假定版本号是1,2,3,4,5,6,7,8,9,10. Revert to this revision: 如果在版本6这里点击“Revert to this revision ...

  3. 发布在IIS上的Web程序,调用服务器的COM组件

    场景大致是这样的,在工厂中分布着许多的PDA点,这些PDA点都要进行实时的扫描--打印操作.实现方法是采用网络打印机,然后服务器安装驱动,管理着所有的打印机.然后服务器,发布一个WebService, ...

  4. whois配置

    $itemRules = array ( 'default' => array ( 'registry_domain_id' => 'Registry Domain ID:(.*?)', ...

  5. TextView的属性列表

    网上收集和自己整理的TextView控件中可选择的属性列表,经常可以用到的: android:autoLink       设置是否当 文本为URL链接/email/电话号码/map时,文本显示为可点 ...

  6. studio--问题

    2,Android studio 怎么能删除干净一个module? 工程→open module settings→选中你要删除的module,然后点“一”,确定后返回,即可在module邮件下看到d ...

  7. Opensturt和Struct区别

    1 OpenStruct和Struct区别,Opestruct你需要创建时,直接给属性赋值,而Struct需要先定义属性后 ,再给属性赋值.选择哪个就看你对属性赋值的时机, 2 Struct和Open ...

  8. 第4章 管道和FIFO

    4.1 管道 管道是由pipe函数创建的,提供一个单向数据流. 头文件 #include <unistd.h> 函数原型 int pipe(int fd[2]); 返回值 成功则为0,出错 ...

  9. Intellij IDEA 使用Spring-boot-devTools无效解决办法

    相信大部分使用Intellij的同学都会遇到这个问题,即使项目使用了spring-boot-devtools,修改了类或者html.js等,idea还是不会自动重启,非要手动去make一下或者重启, ...

  10. C++学习4

    在C++中,定义函数时可以给参数指定一个默认的初始值.调用函数时,可以省略有默认值的参数.也就是说,如果用户指定了参数的值,那么就使用用户指定的值,否则使用参数的默认值. C++规定,默认参数只能放在 ...