Python os 模块知识点整理
2018-03-27 14:04
477 查看
Python os模块知识点整理>
先引用 os.py 的一段介绍
This exports:
all functions from posix, nt or ce, e.g. unlink, stat, etc.
os.path is either posixpath or ntpath
os.name is either ‘posix’, ‘nt’ or ‘ce’.
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 (always ‘.’)
os.altsep is the alternate pathname separator (None or ‘/’)
os.pathsep is the component separator used in $PATH etc
os.linesep is the line separator in text files (‘\r’ or ‘\n’ or ‘\r\n’)
os.defpath is the default search path for executables
os.devnull is the file path of the null device (‘/dev/null’, etc.)
Programs that import and use ‘os’ stand a better chance of being
portable between different platforms. Of course, they must then
only use functions that are defined by all platforms (e.g., unlink
and opendir), and leave all pathname manipulation to os.path
(e.g., split and join).
简单说明:os 模块提供了一个统一的操作系统接口函数, 这些接口函数通常是平台指定的,os 模块能在不同操作系统平台如 nt 或 posix中的特定函数间自动切换,从而能实现跨平台操作。
import os
1 文件操作(常用)
os.path.split( path )
将path分割成目录和文件名二元组
os.path.splitext( path )
将path分割成’.’的前缀和后缀
os.path.basename( path )
返回path最后的文件名。
os.path.isfile( path )
如果path是一个存在的文件,返回True。否则返回False
os.path.getsize( path )
返回path文件的大小。’Bytes’
b = os.path.exists( path )
判断文件是否存在
os.remove( file )
移除文件file
os.rename( oldfileName, newFilename )
文件重命名
os.stat( )
获取文件/目录信息
2 目录操作
os.listdir( path )
返回指定目录下的所有目录和文件
os.getcwd()
获取当前目录
os.makedirs( path )
创建子目录,生成多层递归目录
os.rmdir( path )
删除子目录
os.removedirs( )
若目录为空则删除该目录,并递归到上一级目录
os.chdir( path )
改变当前目录到path,类似于cmd下的shell: cd
os.curdir()
返回当前目录
os.walk()
生成一个目录树下的所有文件名
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
top – 根目录下的每一个文件夹(包含它自己), 产生3-元组 (dirpath, dirnames, filenames)【文件夹路径, 文件夹名字, 文件名】。
topdown –可选,为True或者没有指定, 一个目录的的3-元组将比它的任何子文件夹的3-元组先产生 (目录自上而下)。如果topdown为 False, 一个目录的3-元组将比它的任何子文件夹的3-元组后产生 (目录自下而上)。
onerror – 可选,是一个函数; 它调用时有一个参数, 一个OSError实例。报告这错误后,继续walk,或者抛出exception终止walk。
followlinks – 设置为 true,则通过软链接访问目录。
os.path.join(path1[, path2[, …]])
多个路径组合返回
os.write(fid, str)
写入字符串到文件描述符fd中.返回实际写入的字符串长度。
os.renames(old, new)
递归地对目录进行更名,也可以对文件进行更名
3 其他
os.name
显示当前使用的平台
os.system()
打开一个新的shell
os.sep
更改操作系统中的路径分隔符
os.linesep
字符串给出当前平台使用的行终止符
os.environ
获取系统环境变量
先引用 os.py 的一段介绍
This exports:
all functions from posix, nt or ce, e.g. unlink, stat, etc.
os.path is either posixpath or ntpath
os.name is either ‘posix’, ‘nt’ or ‘ce’.
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 (always ‘.’)
os.altsep is the alternate pathname separator (None or ‘/’)
os.pathsep is the component separator used in $PATH etc
os.linesep is the line separator in text files (‘\r’ or ‘\n’ or ‘\r\n’)
os.defpath is the default search path for executables
os.devnull is the file path of the null device (‘/dev/null’, etc.)
Programs that import and use ‘os’ stand a better chance of being
portable between different platforms. Of course, they must then
only use functions that are defined by all platforms (e.g., unlink
and opendir), and leave all pathname manipulation to os.path
(e.g., split and join).
简单说明:os 模块提供了一个统一的操作系统接口函数, 这些接口函数通常是平台指定的,os 模块能在不同操作系统平台如 nt 或 posix中的特定函数间自动切换,从而能实现跨平台操作。
import os
1 文件操作(常用)
os.path.split( path )
将path分割成目录和文件名二元组
>>> path = 'd:\Learn\MATLAB\CH02\Fig0203(a).tif' >>> os.path.split(path) ('d:\\Learn\\MATLAB\\CH02', 'Fig0203(a).tif')
os.path.splitext( path )
将path分割成’.’的前缀和后缀
>>> os.path.splitext(path) ('d:\\Learn\\MATLAB\\CH02\\Fig0203(a)', '.tif')
os.path.basename( path )
返回path最后的文件名。
>>> os.path.basename(path) 'Fig0203(a).tif'
os.path.isfile( path )
如果path是一个存在的文件,返回True。否则返回False
>>> os.path.isfile(path) True
os.path.getsize( path )
返回path文件的大小。’Bytes’
>>> os.path.getsize(path) 1146614
b = os.path.exists( path )
判断文件是否存在
>>> os.path.exists(path) True
os.remove( file )
移除文件file
os.rename( oldfileName, newFilename )
文件重命名
os.stat( )
获取文件/目录信息
>>> os.stat(path) os.stat_result(st_mode=33206, st_ino=281474976710784, st_dev=1040830797, st_nlink=1, st_uid=0, st_gid=0, st_size=1146614, st_atime=1512965527, st_mtime=1053245450, st_ctime=1512965527)
2 目录操作
os.listdir( path )
返回指定目录下的所有目录和文件
os.getcwd()
获取当前目录
>>> os.chdir('d:\\') >>> os.getcwd() 'd:\\'
os.makedirs( path )
创建子目录,生成多层递归目录
os.rmdir( path )
删除子目录
os.removedirs( )
若目录为空则删除该目录,并递归到上一级目录
os.chdir( path )
改变当前目录到path,类似于cmd下的shell: cd
>>> os.chdir('d:\\') >>> os.getcwd() 'd:\\'
os.curdir()
返回当前目录
>>> os.curdir '.'
os.walk()
生成一个目录树下的所有文件名
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
top – 根目录下的每一个文件夹(包含它自己), 产生3-元组 (dirpath, dirnames, filenames)【文件夹路径, 文件夹名字, 文件名】。
topdown –可选,为True或者没有指定, 一个目录的的3-元组将比它的任何子文件夹的3-元组先产生 (目录自上而下)。如果topdown为 False, 一个目录的3-元组将比它的任何子文件夹的3-元组后产生 (目录自下而上)。
onerror – 可选,是一个函数; 它调用时有一个参数, 一个OSError实例。报告这错误后,继续walk,或者抛出exception终止walk。
followlinks – 设置为 true,则通过软链接访问目录。
os.path.join(path1[, path2[, …]])
多个路径组合返回
os.write(fid, str)
写入字符串到文件描述符fd中.返回实际写入的字符串长度。
os.renames(old, new)
递归地对目录进行更名,也可以对文件进行更名
3 其他
os.name
显示当前使用的平台
>>> os.name 'nt'
os.system()
打开一个新的shell
>>> os.system('Hello') 1
os.sep
更改操作系统中的路径分隔符
>>> os.sep '\\'
os.linesep
字符串给出当前平台使用的行终止符
>>> os.linesep '\r\n'
os.environ
获取系统环境变量
相关文章推荐
- Python OS 模块操作整理
- Python模块整理(一):文件系统相关模块os
- python中的os模块【知识点】
- python模块整理1-os模块
- python中的os模块指令
- python os.path模块
- python random模块整理
- python random模块整理
- python模块整理23-hashlib
- # 输出目录下的文件 os模块 分类: python 小练习 2013-05-13 18:01 205人阅读 评论(0) 收藏
- Python笔记:常用模块(os/sys/platform)
- python os.path模块
- python中对文件、文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块
- python os模块
- Python中的os模块
- python-os模块
- python os模块和os.path模块相关功能
- Python os模块介绍
- [转]python os模块 常用命令
- Python OS 文件操作模块常用函数