] [Python standard library variety of operating systems and common interfaces path operations library - os and os.path

Welcome to the Python team official document translation: https://www.transifex.com/python-doc/


os Module Introduction: mainstream operating system Windows, UNIX, Mac OS, etc., os module provides support related functions to access a variety of operating systems, involving realization of the files related to operating functions, operating system access Path path, shell command obtain hardware-related information line operation, Linux operating extended attributes, process management, CPU, etc., based on real operations and related operating systems offer some random number of system variables.

os.path Module Introduction: os.path module is used to get the file attributes, and implements some useful functions on pathnames.

Contact with the distinction between these two modules can refer to: "Python: Load the difference between the association and the os and os.path"

os module official document: https://docs.python.org/3/library/os.html

os.path module official document: https://docs.python.org/3/library/os.path.html

Some common function module os:

function Features
os.environ Get the current operating system environment variables
os.sep Gets the operating system used to separate the different parts of the character path, Windows is //, Linux is\
os.name Get the current name of the working platform, Windows is nt, Linux isposix
os.getenv(key, default=None) Gets an environment variable, if not return none, key, default and return values ​​are of type string str
os.getcwd() Get the current path is located
os.system(command) Run in the sub-shell (command is a command string)
os.urandom (n) Obtaining a size of n bytes for the random number using the encryption string
os.listdir(path=’.’) Return all files and directories under the specified directory
os.mkdir(path) Create a directory, if the directory already exists, an exception is thrown FileExistsError
os.makedirs(path) Create a multi-layer recursive directory, if the directory are all present, an exception is thrown FileExistsError
os.rmdir(path) To delete a empty directory, if the directory does not exist or is not empty, it will be thrown FileNotFoundError are abnormal or OSError
os.removedirs(path) Delete multilayer recursive empty directory, if there are files in the directory can not be deleted
os.chdir(path) Change the current directory to the specified directory
os.rename(src, dst) The file or directory dst src rename
On Windows, if dst already exists, an exception is thrown FileExistsError
on Unix, if the file src and dst is a directory, an exception will be thrown IsADirectoryError, otherwise an exception is thrown NotADirectoryError
in Unix on both the directory and if dst is empty, dst are silently replaced. If a non-empty directory dst, OSError exception is thrown
on Unix, if both file, in a case where the user has authority, will silently Alternatively dst
if src and dst on different file systems, the present on some Unix operating branch may fail

Some common function module os.path:

function Features
os.path.abspath(path) Returns the absolute path
os.path.basename(path) Returns the file name
os.path.commonprefix(list) Return list (multiple paths), all of the common path of the longest path
os.path.dirname(path) Returns the file path
os.path.exists(path) If the path path exists, returns True; path if the path does not exist, returns False
os.path.lexists Returns True path exists, the path returns True damage
os.path.expanduser(path) The path contained in ~and ~userconverted into a user directory
os.path.expandvars(path) Alternatively path based on the value contained in the environment variables $nameand${name}
os.path.getatime (path) Returns the last access time (floating-point number of seconds)
os.path.getmtime(path) Returns last file modification time
os.path.getctime(path) 返回文件 path 创建时间
os.path.getsize(path) 返回文件大小,如果文件不存在就返回错误
os.path.isabs(path) 判断是否为绝对路径
os.path.isfile(path) 判断路径是否为文件
os.path.isdir(path) 判断路径是否为目录
os.path.islink(path) 判断路径是否为链接
os.path.ismount(path) 判断路径是否为挂载点
os.path.join(path1[, path2[, …]]) 把目录和文件名合成一个路径
os.path.normcase(path) 转换 path 的大小写和斜杠
os.path.normpath(path) 规范 path 字符串形式
os.path.realpath(path) 返回 path 的真实路径
os.path.relpath(path[, start]) 从 start 开始计算相对路径
os.path.samefile(path1, path2) 判断目录或文件是否相同
os.path.sameopenfile(fp1, fp2) 判断 fp1 和 fp2 是否指向同一文件
os.path.samestat(stat1, stat2) 判断 stat tuple stat1 和 stat2 是否指向同一个文件
os.path.split(path) 把路径分割成 dirname 和 basename,返回一个元组
os.path.splitdrive(path) 一般用在 windows 下,返回驱动器名和路径组成的元组
os.path.splitext(path) 分割路径,返回路径名和文件扩展名的元组
os.path.splitunc(path) 把路径分割为加载点与文件
os.path.walk(path, visit, arg) 遍历path,进入每个目录都调用 visit 函数,visit 函数必须有3个参数 (arg, dirname, names),dirname 表示当前目录的目录名,names 代表当前目录下的所有文件名,args 则为 walk 的第三个参数
os.path.supports_unicode_filenames 设置是否支持 unicode 路径名
发布了156 篇原创文章 · 获赞 567 · 访问量 48万+

Guess you like

Origin blog.csdn.net/qq_36759224/article/details/104462568