Loading

OS模块

os模块

本模块提供了一种使用与操作系统相关的功能的便捷式途径。

  • os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
>>> import os
>>> os.getcwd()
'D:\\Desktop\\Netfile
  • os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cd
>>> os.chdir('D:/')  # 也可使用相对路径>>> os.chdir('../../../')
>>> os.getcwd()
'D:\\'
  • os.curdir 返回当前目录:.
>>> os.curdir
'.'
  • os.pardir 获取当前目录的父目录字符串名:..
>>> os.pardir
'..'
  • os.mkdir('dirname') 生成单级目录;相当于shell中mkdir dirname
>>> os.mkdir('d1')
>>> os.listdir()
[...'d1', ...]
  • os.makedirs('dirname1/dirname2') 可生成多层递归目录
>>> os.makedirs('d1/d2/d3')
>>> os.listdir('d1')
['d2']
>>> os.listdir('d1/d2')
['d3']
  • os.removedirs('dirname1') 目录内有文件无法删除,若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推。
>>> os.removedirs('d1/d2/d3')
>>> os.listdir('d1')
FileNotFoundError: [WinError 3] 系统找不到指定的路径。: 'd1'
  • os.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
>>> os.makedirs('d1/d2')
>>> os.rmdir('d1')
OSError: [WinError 145] 目录不是空的。: 'd1'
  • os.remove() 删除一个文件
>>> os.remove('a.txt')
  • os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
>>> os.listdir('E:/')
['$RECYCLE.BIN', 'Google', 'Google下载', 'Honeyview', 'System Volume Information']
  • os.rename("oldname","newname") 重命名文件/目录
>>> os.rename('d1','d2')
  • os.stat('path/filename') 获取文件/目录信息
>>> os.stat('d2')
os.stat_result(st_mode=16895, st_ino=844424930218800, st_dev=781950587, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1610350677, st_mtime=1610350677,
 st_ctime=1610350677)
  • os.sep 输出操作系统特定的路径分隔符,win下为"\",Linux下为"/"
>>> os.sep
'\\'
  • os.linesep 输出当前平台使用的行终止符,win下为"\r\n",Linux下为"\n"
>>> os.linesep
'\r\n'
  • os.pathsep 输出用于分割文件路径的字符串 win下为;,Linux下为:
>>> os.pathsep
';'
  • os.name 输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
>>> os.name
'nt'
  • os.system("bash command") 运行shell命令,直接显示,没有返回值。
>>> os.system('dir')  # 列出当前目录下所有文件
  • os.path.abspath(path) 返回path规范化的绝对路径
>>> os.path.abspath('Z:/a\\b/c\\d')
'Z:\\a\\b\\c\\d'
  • os.path.split(path) 将path分割成目录和文件名二元组返回
>>> os.path.split('Z:\\a\\b\\c\\d')
('Z:\\a\\b\\c', 'd')
  • os.path.dirname(path) 返回path的目录。就是os.path.split(path)的第一个元素
>>> os.path.dirname('Z:\\a\\b\\c\\d')
'Z:\\a\\b\\c'
  • os.path.basename(path) 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素.
>>> os.path.basename('Z:\\a\\b\\c\\d')
'd'
  • os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False
>>> os.path.exists('Z:\\a\\b\\c\\d')
False
>>> os.path.exists('D:')
True
  • os.path.isabs(path) 如果path是绝对路径,返回True
>>> os.path.isabs('Z:\\a\\b\\c\\d')
True
>>> os.path.isabs('D:')
False
  • os.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回False
>>> os.path.isfile('Z:\\a\\b\\c\\d')
False
>>> os.path.isfile('D:')  # 必须是文件
False
  • os.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回False
>>> os.path.isdir('D:')
True
  • os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
>>> os.path.join('a','b','c')
'a\\b\\c'
>>> os.path.join('D:','a','b')
'D:a\\b'
>>> os.path.join('a','b','D:','c')
'D:c'
>>> os.path.join('a','b','D:/','/c')  # 第一个绝对路径之前的参数将被忽略
'D:/c'
  • os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间。
>>> os.path.getatime('d2')
1610350677.7029717
  • os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间。
>>> os.path.getmtime('d2')
1610350677.7029717
  • os.path.getsize(path) 返回path的大小,单位字节。
>>> os.path.getsize('day04/test.py')
7854
  • os.environ 获取系统环境变量字典,可通过字典设置系统环境变量。(常用)
# 获取全部系统环境变量
>>> os.environ
environ({'NUMBER_OF_PROCESSORS': '4', 'COMMONPROGRAMFILES': 'C:\\Program Files\\Common Files', 'PROMPT': '$P$G', 'SESSIONNAME': 'Console',...})
# 获取指定系统环境变量
>>> os.environ.get("OS")
'Windows_NT'
# 添加变量
>>> os.environ['PASSWORD']='123'
>>> os.environ.get('PASSWORD')
'123'

添加的变量仅在解释器本次执行过程中生效,解释器关闭则失效。

posted @ 2021-01-12 15:11  吃了好多肉  阅读(101)  评论(0编辑  收藏  举报