python中模块sys与os的一些常用方法

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命令:


 

原文链接: https://www.cnblogs.com/yinheyi/p/6051418.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    python中模块sys与os的一些常用方法

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/395619

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月4日 上午9:52
下一篇 2023年4月4日 上午9:53

相关推荐