sys模块提供了访问或操作与python解释器相关方法与对象。

我们就列举出常用到的知识,以后,随着学习,不断补充。

几个常用到的动态对象:

sys.argv,这是一个列表,它包含了所有传递给脚本的命令行参数,其中第一个为脚本自身的名称呀;

sys.path 这也是一个列表,里面放了模块的搜索路经。并且呢,path[0]表示当脚本的路经。

sys.modules, 这是一个字典类型,它里面放了所有载入的模块。

sys.stdin , 标准输入流--一个类文件对象, raw_input()与input()这是使用它。

sys.stdout,标准输出流--一个类文件对象,我们使用print的时候,就是在使用它;

sys.stderr:标准错误流--一个类文件对象

(以上三个流对象都可以重定向到其它 的IO设备的。)

几个常用的静态对象:

sys.builtin_module_names: 这是一个元组,里面放的是python解释器的内置的所有模块名称。

sys.version ,存放了python解释器的版本信息。

sys.platform , 存放的是python解释器的平台,返回linux2或windows

常用到的函数:

exit([status]),退出,默认为0,表示正常退出。

对于os模块来说,首说一个通过os来调用的模块:os.path,它存在于os模块中,本身又是一个模块。os.path只是一个别名而已,对于不同的平台,这个模块可能有不同的名字。如,posix式的系统(如unix,linux)的话,它的别名就叫做os.path.对于Mac、windows等来说,可能名字叫做macpach, ntpath。

模块os.path下常见的函数:

abspath(path):    Return an absolute path,返回绝对路经
basename(p):     Returns the final component of a pathname:返回路经的最后一部分,即 最后一个 / 后的内容;

commonprefix(list):    Given a list of pathnames, returns the longest common leading componen,它的输入为一个路经

的列表,用于返回list中,所有path共有的最长的路径,从左向右,相同字符。

dirname(p):   Returns the directory component of a pathname,返回目录哦;

exists(path):    Test whether a path exists.  Returns False for broken symbolic links,测试一个目录是否存在;

expanduser(path):    Expand ~ and ~user constructions.  If user or $HOME is unknown,do nothing.作用就是把目录

中的~展开;

getatime(filename):  获得最后一次访问文件的时间,可以通过 os.stat()函数查看具体的状态;

getctime(filename):   返回元数据最后一次change的时间;

getmtime(filename):  返回最后一次修改的时间;

isabs(s):      测试一个路经是否是绝对路经;

isdir(s) :        Return true if the pathname refers to an existing directory.

isfile(path):    Test whether a path is a regular file

join(path1[, path2[, ...]]); 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略normcase(path): 在Linux下,该函数会原样返回path,在windows平台上会将路径中所有字符转换为小写,并将所有斜杠转换 为反斜杠
splitdrive(path): 拆分驱动器名和路径,主要对win,对linux元组第一个总是空的
splitext(path): 分离文件名与扩展名;默认返回(fname,fextension)元组,可做分片操作 ,以“.”为分隔符
getsize(path): 返回path的大小(字节)

在os模块中,相关的函数太多了,只说几个有用的,如果想具体看,可以通过 help(‘os’)查看哦;

 

一些与平台相关的一些常量,平台一一样,返回的值也不一样;

     - os.name is 'posix', 'nt', 'os2', 'ce' or 'riscos' ,操作系统的名字;
     - os.curdir is a string representing the current directory ('.' or ':') 当前目录的表示;
     - os.pardir is a string representing the parent directory ('..' or '::') 父目录的表示;
     - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\')目录名的分隔号;
     - os.extsep is the extension separator ('.' or '/') 名字与扩展名之间的分隔号;
     - os.altsep is the alternate pathname separator (None or '/')
     - os.pathsep is the component separator used in $PATH etc 目录之间的分隔号,linux下为:。
     - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')一行的分隔号;
     - os.defpath is the default search path for executables,执行程序时的默认路经;linux下通常为bash的路经
     - os.devnull is the file path of the null device ('/dev/null', etc.) 空设备的文件路经;
  
 
一些常用函数:
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 
os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cd
os.pardir 获取当前目录的父目录字符串名:('..')
os.makedirs('dirname1/dirname2') 可生成多层递归目录
os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
os.mkdir('dirname') 生成单级目录;相当于shell中mkdir dirname
os.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
os.remove() 删除一个文件
os.rename("oldname","newname") 重命名文件/目录
os.stat('path/filename') 获取文件/目录信息
os.symlink('path/filename','ln_filename') 创建符号链接,源需绝对路径
os.utime() 修改时间属性

os.system(command) 函数用来运行shell命令:

最新文章

  1. CozyRSS开发记录21-默认RSS源列表
  2. 例子:Camera Color Picker Sample (YCbCr->ARGB)
  3. UIProgressView(进度条控件)
  4. 网站通用登录模块代码 分类: ASP.NET 2014-12-06 10:49 615人阅读 评论(0) 收藏
  5. 仿IOS 开关按钮
  6. docker offical docs:Working with Docker Images
  7. RPC的学习 & gprotobuf 和 thrift的比较
  8. poj 1695 动态规划
  9. HDOJ2001计算两点间的距离
  10. 安装和使用screen
  11. 给定任意一个字符串,使用 for in 语句来统计字符出现的个数
  12. java.text.NumberFormat使用方法
  13. 经验总结35--IP地址区域匹配
  14. 【Java框架型项目从入门到装逼】第十四节 查询用户列表展现到页面
  15. 苹果新的编程语言 Swift 语言进阶(一)--综述
  16. 用stringstream可以用来分割空格、tab、回车换行隔开的字符串:
  17. expect login ssh
  18. ASP.NET MVC下使用AngularJs语言(八):显示html
  19. [Java] LinkedHashMap 源码简要分析
  20. JsonKey小写

热门文章

  1. HDU(2255),KM算法,最大权匹配
  2. lua注释
  3. qbxt十一系列一
  4. 电脑能上网,手机连上wifi不能上网
  5. javascript耐人寻味
  6. 5-JS函数
  7. U盘安装CentOS7的最终解决方案
  8. DataList数据的绑定
  9. 【leetcode❤python】 257. Binary Tree Paths
  10. BZOJ 1266 上学路线route(最小割)