Python OS module

Source: Internet
Author: User

OS Module

Abbreviation for OS operating system

The OS module is the operation of the operating system, which must first be imported into the module:

import os
function getcwd () in the OS module
功能:获取当前的工作目录格式:os.getcwd()返回值:路径字符串
ChDir ()
功能:修改当前工作目录格式:os.chdir()返回值:None
Listdir ()
功能:获取指定文件夹中的所有文件和文件夹组成的列表格式:os.listdir(目录路径)返回值:目录中内容名称的列表
mkdir ()
功能:创建一个目录/文件夹格式:os.mkdir(目录路径)返回值:None
Makedirs ()
功能:递归创建文件夹格式:os.makedirs(路径)
RmDir ()
功能:移除一个目录(必须是空目录)格式:os.rmdir(目录路径)返回值:None
Removedirs ()
功能:递归删除文件夹格式:os.removedirs(目录路径)返回值:None

Note: such as: Delete d:/a/b/c

  • If there is no other file or folder in the ABC folder except for the folder shown in the path, Removedirs removes all folders a,b,c
  • If any folder in the ABC contains other files and folders, then the folder will not be deleted, if the bottom of the C folder will be a non-empty error!
Rename ()
功能:修改文件和文件夹的名称格式:os.rename(源文件或文件夹,目标文件或文件夹)返回值:None
Stat ()
功能:获取文件的相关信息格式:os.stat(文件路径)返回值:包含文件信息的元组
System ()
执行系统命令格式:os.system()返回值:整型慎用! 不小心来个 rm -rf / 欲哭无泪!
Getenv ()
功能:获取系统环境变量格式:os.getenv(获取的环境变量名称)返回值:字符串
Putenv ()
功能:设置系统环境变量格式:os.putenv(‘环境变量名称‘,值)返回值:无注意:putenv确实可以添加成功,但是无法使用正常的getenv检测到
Exit ()
功能:推出当前执行命令,直接关闭当前操作格式:exit()返回值:无

The value of the current OS module CurDir
os.curdir功能:获取当前路径   都是.
Pardir
os.pardir功能:获取上层目录路径 都是..
Path
os.path功能:os中的一个子模块,操作非常多
Name
os.name功能:当前系统的内核名称  win->nt  linux/unix->posix
Sep
os.sep功能:获取当前系统的路径分割符号 window -> \  linux/unix -> /
Extsep
os.extsep功能:获取当前系统中文件名和后缀之间的分割符号,所有系统都是.
Linesep
os.linesep功能:获取当前系统的换行符号 window -> \r\n  linux/unix -> \n
Os.environ Module
os.environ可以直接获取所有环境变量的信息组成的字典,如果希望更改环境变量,并且可以查询得到,就需要对os.environ进行操作该模块的所有方法均是字典的方法,可以通过字典的os.environ的结果进行操作。注意:无论使用os.getenv,putenv 还是使用os.environ进行环境变量的操作,都是只对当前脚本,临时设置而已,无法直接更新或者操作系统的环境变量设置。
Os.path Module
os.path是os模块中的子模块,包含很多和路径相关的操作
function Section Abspath ()
功能:将一个相对路径转化为绝对路径格式:os.path.abspath(相对路径)返回值:绝对路径字符串
BaseName ()
功能:获取路径中的文件夹或者文件名称(只要路径的最后一部分)格式:os.path.basename(路径)返回值:路径的最后一部分(可能是文件名也可能是文件夹名)
DirName ()
功能:获取路径中的路径部分(出去最后一部分)格式:os.path.dirname(路径)返回值:路径中除了最后一部分的内容字符串
Join ()
功能:将2个路径合成一个路径格式:os.path.join(路径1,路径2)返回值:合并之后的路径
Split ()
功能:将一个路径切割成文件夹和文件名部分格式:os.path.split(路径)返回值:元组
Splitext ()
功能:将一个文件名切成名字和后缀两个部分格式:os.path.splitext(文件名称)返回值:元组(名称,后缀)
GetSize ()
功能:获取一个文件的大小格式:os.path.getsize(路径)返回值:整数
Isfile ()
功能:检测一个路径是否是一个文件格式:os.path.isfile(路径)返回值:布尔值
Isdir ()
功能:检测一个路径是否是一个文件夹格式:os.path.isdir(路径)返回值:布尔值
Getctime ()
功能:获取文件的创建时间 (get create time)格式:os.path.getctime(文件路径)返回值:时间戳浮点数
Getmtime ()
功能:获取文件的修改时间(get modify time)格式:os.path.getmtime(文件路径)返回值:时间戳浮点数
Getatime ()
功能:获取文件的访问时间(get active time)格式:os.path.getatime(文件路径)返回值:时间戳浮点数
Exists ()
功能:检测指定的路径是否存在格式:os.path.exists(路径)返回值:布尔值
Isabs ()
功能:检测一个路径是否是绝对路径格式:os.path.isabs(路径)返回值:布尔值
Islink ()
功能:检测一个路径是否是链接格式:os.path.islink(路径)返回值:布尔值
Samefile ()
功能:检测2个路径是否指向同一个文件格式:os.path.samefile(路径1,路径2)返回值:布尔值
 
 

Python OS module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.