Python---OS module

Source: Internet
Author: User
Tags posix python script

Import OS module is required when using OS modules

OS.GETCWD () module functions

Function: Gets the current working directory, that is, the directory path of the current Python script work "no parameter"

How to use:os.getcwd ()

Format such as: a = OS.GETCWD ()

#!/usr/bin/env python #-*-Coding:utf8-*-import os a = OS.GETCWD ()     #获取当前工作目录, that is, the directory path of the current Python script work "no parameter" print (a) #输出 #H : \py

Os.chdir () module functions

Function: Changes the current script working directory, equivalent to the shell under the CD "Ginseng"

How to use:os.chdir ("Path to change in string format")

Format such as: Os.chdir ("D:")

#!/usr/bin/env python #-*-Coding:utf8-*-import os retval = OS.GETCWD ()    # View the current working directory print ("Current working directory is%s"% retval) os. ChDir ("D:")    # Modify Current working directory retval = OS.GETCWD ()    # View modified working directory print ("Directory modification succeeded%s"% retval) #输出 # Current working directory is H:\py # Directory modified successfully D:\

Os.curdir module functions

Function: Returns the current directory: ('. ') "No Reference"

How to use:os.curdir

Format such as: a = Os.curdir

#!/usr/bin/env python #-*-Coding:utf8-*-import os a = Os.curdir   #返回当前目录: ('. ') print (a) #输出 #.

Os.pardir module functions

Function: Gets the parent directory string name of the current directory: (' ... ') "No Reference"

How to use:os.pardir

Format such as: a = Os.pardir

#!/usr/bin/env python #-*-Coding:utf8-*-import os a = Os.pardir   #获取当前目录的父目录字符串名: (' ... ') print (a) #输出 #..

Os.makedirs () module functions

Function: can create multi-level recursive directory "with reference"

How to use:os.makedirs ("directory name or path to create")

Format such as: Os.makedirs ("Div1/div2/div3")

#!/usr/bin/env python #-*-Coding:utf8-*-import os os.makedirs ("Div1/div2/div3")   #可创建多层递归目录

Os.removedirs () module functions

Function: If the directory is empty, then delete, and recursively to the previous level of the directory, if also empty, then delete, and so on "has the reference"

How to use:os.removedirs ("directory name or path to delete")

Format such as: Os.removedirs ("Div1/div2/div3")

#!/usr/bin/env python #-*-Coding:utf8-*-import os os.removedirs ("Div1/div2/div3")   #若目录为空, then delete, and recursively to the previous level of the directory, if also empty, Delete, and so on "with reference"

Os.mkdir () module functions

Function: Create a single-level directory, equivalent to the shell "mkdir"

How to use:os.mkdir ("directory name to create")

Format such as: Os.mkdir ("DSR")

#!/usr/bin/env python #-*-Coding:utf8-*-import os os.mkdir ("DSR")   #生成单级目录; equivalent to the shell mkdir "ginseng"

Os.rmdir () module functions

Function: Delete the single-level empty directory, if the directory is not empty can not be deleted, error, equivalent to the shell rmdir "ginseng"

How to use:os.rmdir ("Directory name to remove")

Format such as: Os.rmdir ("DSR")

#!/usr/bin/env python #-*-Coding:utf8-*-import os os.rmdir ("DSR")   #删除单级空目录, if the directory is not empty can not be deleted, error, equivalent to the shell rmdir "ginseng"

Os.listdir () module functions

Features: Lists all files and subdirectories under the specified directory, including hidden files, and prints "ginseng" as a list

How to use:os.listdir ("directory name")

Format: a = Os.listdir ("Lib")

#!/usr/bin/env python #-*-Coding:utf8-*-import os a = Os.listdir ("Lib")   #列出指定目录下的所有文件和子目录, including hidden files, and print "parametric" pri as a list NT (a) #输出文件名称和目录名称 # [' mk.py ', ' __pycache__ ']

Os.remove () module functions

Function: Delete a file, the directory does not work "ginseng"

How to use:os.remove ("Directory name to remove")

Format such as: Os.remove ("2.txt")

#!/usr/bin/env python #-*-Coding:utf8-*-import os os.remove ("2.txt")   #删除一个文件

Os.rename () module functions

Function: Rename file or directory "with reference"

How to use:os.rename ("File or directory name", "file or directory name to be modified")

Formats such as: Os.rename ("1.txt", "2.txt")

#!/usr/bin/env python #-*-Coding:utf8-*-import os os.rename ("1.txt", "2.txt")   #重命名文件或目录

Os.stat () module function "Common"

Function: Get file or directory information "with reference"

How to use:os.stat ("file or directory name to get information")

Format: a = Os.stat ("22")

#!/usr/bin/env python #-*-Coding:utf8-*-import os a = Os.stat ("#获取文件或者目录信息")   print (a)

OS.SEP module functions

Function: Operating system-specific path delimiter, win under "\", Linux for "/" "No parameter"

How to use: Os.sep

Format such as: a = Os.sep

#!/usr/bin/env python #-*-Coding:utf8-*-import os a = Os.sep   #操作系统特定的路径分隔符, win under "\", Linux for "/" Print (a)

OS.LINESEP module functions

Function: The current platform uses a newline character, win under "\t\n", Linux for "\ n" "No parameter"

How to use:os.linesep

Format such as: a = Os.linesep

#!/usr/bin/env python #-*-Coding:utf8-*-import os a = Os.linesep   #当前平台使用的行终止符, win under "\t\n", Linux "\ n" b = "Hello,%s in?" "% (a)   #相当于加了 \ n newline character print (b) #输出 # hello # are you there?

OS.PATHSEP module functions

Function: The string "no parameter" used to split the file path

How to use:os.pathsep

Format such as: a = Os.pathsep

#!/usr/bin/env python #-*-Coding:utf8-*-import os a = Os.pathsep   #用于分割文件路径的字符串 print (a) #输出 #;

Os.name module functions

Function: string indicates the current use of the platform. Win-> ' NT '; Linux-> ' POSIX '"no Reference"

How to use:os.name

Format such as: a = Os.name

#!/usr/bin/env python #-*-Coding:utf8-*-import os a = Os.name   #字符串指示当前使用平台. Win-> ' NT '; Linux-> ' POSIX ' print (a) #输出 # NT

Os.system () module functions

Function: Run the shell command, directly display the "ginseng"

How to use:os.system ("Shell system Command ")

Format such as: Os.system ("Bash command")

#!/usr/bin/env python #-*-Coding:utf8-*-import os os.system ("Bash command")   #运行shell命令, direct display

Os.environ module functions

Function: Get system environment variable "no parameter"

How to use:Os.environ

Format such as: a = Os.environ

#!/usr/bin/env python #-*-Coding:utf8-*-import os a = Os.environ   #获取系统环境变量 for I in a:     #循环出系统环境变量     Print (i) #输出 # userprofile # COMSPEC # PYTHONPATH # processor_level # OS # USERNAME # allusersprofile # programfiles (X86) # Progra  Mfiles # number_of_processors # PROGRAMDATA # localappdata # public # psmodulepath # commonprogramfiles (X86) # homedrive # SessionName # logonserver # pathext # commonprogramfiles # systemdrive # windir # COMPUTERNAME # Userdomain_roamingprofil E # PATH # pythonioencoding # SYSTEMROOT # COMMONPROGRAMW6432 # processor_architecture # userdomain # pycharm_hosted # APP DATA # TEMP # PROGRAMW6432 # processor_identifier # homepath # processor_revision # pythonunbuffered # TMP

Os.path.abspath () module functions

Function: Returns the canonical absolute path of a file or directory "with reference"

How to use:os.path.abspath ("file or directory name to return canonical absolute path")

Format as: a = Os.path.abspath ("mk.py")

#!/usr/bin/env python #-*-Coding:utf8-*-import os a = Os.path.abspath ("mk.py")   #返回一个文件或者目录的规范化的绝对路径 Print (a) #输出 # H:\py\mk.py

Os.path.split () module functions

Functions: Splitting files into directories and filenames two tuples return "ginseng"

How to use:os.path.split ("Path or name")

Format as: a = Os.path.split ("mk.py")

#!/usr/bin/env python #-*-Coding:utf8-*-import os a = Os.path.split ("mk.py")   #将path分割成目录和文件名二元组返回 Print (a) #输出 # ( ', ' mk.py ')

Os.path.dirname () module function "Common"

Function: Returns the file's directory "has the parameter"

How to use:os.path.dirname ("File path")

Format as: a = Os.path.dirname ("py/lib/mk.py")

#!/usr/bin/env python #-*-Coding:utf8-*-import os a = Os.path.dirname ("py/lib/mk.py")   #返回文件的目录 Print (a) #输出 # py/l Ib

Os.path.basename () module functions

Function: Returns the last file name of the file. How to file with/or \ End "With reference"

How to use:os.path.basename ("File path")

Format as: a = Os.path.basename ("py/lib/mk.py")

#!/usr/bin/env python #-*-Coding:utf8-*-import os a = Os.path.basename ("py/lib/mk.py")   #返回文件最后的文件名. How to file with/or \ End Print (a) #输出 # mk.py

Os.path.exists () module function "Common"

Function: Returns True if the file exists, returns False if the file does not exist

How to use:os.path.exists ("File path")

Format as: a = os.path.exists ("h:/py/lib/mk.py")

#!/usr/bin/env python #-*-Coding:utf8-*-import os a = Os.path.exists ("h:/py/lib/mk.py")   #如果文件存在, returns True if the file does not exist, Returns false Print (a) #输出 # True

Os.path.isabs () module function "Common"

Function: If the file is an absolute path, returns true "parameter"

How to use:os.path.isabs ("File path")

Format as: a = Os.path.isabs ("h:/py/lib/mk.py")

#!/usr/bin/env python #-*-Coding:utf8-*-import os a = Os.path.isabs ("h:/py/lib/mk.py")   #如果文件是绝对路径, returns True Print (a) #输出 # True

Os.path.isfile () module function "Common"

Function: Returns True if the file is a file that exists. Otherwise, false "parameter" is returned.

How to use:os.path.isfile ("File path")

Format as: a = Os.path.isfile ("h:/py/lib/mk.py")

#!/usr/bin/env python #-*-Coding:utf8-*-import os a = Os.path.isfile ("h:/py/lib/mk.py")   #如果文件是一个存在的文件, returns True. Otherwise returns false print (a) #输出 # True

Os.path.isdir () module function "Common"

Function: Returns True if the directory is a directory that exists. Otherwise, false "parameter" is returned.

How to use:os.path.isdir ("directory path")

Format as: a = Os.path.isdir ("H:/py/lib")

#!/usr/bin/env python #-*-Coding:utf8-*-import os a = Os.path.isdir ("H:/py/lib")   #如果目录是一个存在的目录, returns True. Otherwise returns false print (a) #输出 # True

Os.path.join () module function "Common"

Function: Combine multiple string paths to return a full path, and parameters before the first absolute path will be ignored "parameter"

How to use:os.path.join ("File path")

Format: a = Os.path.join (S1,S2,S3,S4)

#!/usr/bin/env python #-*-Coding:utf8-*-import os s1 = "H:" s2 = "py" s3 = "Lib" s4 = "mk.py" a = Os.path.join (s1,s2,s 3,S4)   #将多个字符串路径组合成一个完整的路径返回, parameters prior to the first absolute path will be ignored #输出 #H:p the "parameter" print (a) y\lib\mk.py

Os.path.getatime () module functions

Function: Returns the last access timestamp of the file or directory to which it is pointing "has a reference"

How to use:os.path.getatime ("File path")

Format as: a = Os.path.getatime ("H:/py/lib")

#!/usr/bin/env python #-*-coding:utf8-*-import os import time a = Os.path.getatime ("H:/py/lib")   #返回所指向的文件或者目录的最后存 Take timestamp B = Time.strftime ("%y-%m-%d%h:%m:%s", Time.gmtime (a))   #将时间戳转换成时间格式 print (b) #输出 # 2016-08-22 20:41:28

Os.path.getmtime () module functions

Function: Returns the last access timestamp of the file or directory to which it is pointing "has a reference"

How to use:os.path.getmtime ("File path")

Format as: a = Os.path.getmtime ("H:/py/lib")

#!/usr/bin/env python #-*-coding:utf8-*-import os import time a = Os.path.getmtime ("H:/py/lib")   #返回所指向的文件或者目录的最后修改 Timestamp B = Time.strftime ("%y-%m-%d%h:%m:%s", Time.gmtime (a))   #将时间戳转换成时间格式 print (b) #输出 # 2016-08-22 20:41:28

Os.stat (). st_size Check the size of a file, return bytes

How to use:os.stat (the file path or file name to check for size). St_size

Format: Os.stat ("32.png"). St_size

DX = Os.stat ("32.png"). St_size #检测要发送文件的大小

os.getcwd()                 获取当前工作目录,即当前python脚本工作的目录路径

os.chdir("dirname")         改变当前脚本工作目录;相当于shell下cd os.curdir                   返回当前目录: (‘.‘) os.pardir                   获取当前目录的父目录字符串名:(‘..‘) os.makedirs(‘dir1/dir2‘)    可生成多层递归目录 os.removedirs(‘dirname1‘)   若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推 os.mkdir(‘dirname‘)         生成单级目录;相当于shell中mkdir dirname os.rmdir(‘dirname‘)         删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname os.listdir(‘dirname‘)       列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印 os.remove()                 删除一个文件 os.rename("oldname","new")  重命名文件/目录 os.stat(‘path/filename‘)    获取文件/目录信息 os.sep                      操作系统特定的路径分隔符,win下为"\\",Linux下为"/" os.linesep                  当前平台使用的行终止符,win下为"\t\n",Linux下为"\n" os.pathsep                  用于分割文件路径的字符串 os.name                     字符串指示当前使用平台。win->‘nt‘; Linux->‘posix‘ os.system("bash command")   运行shell命令,直接显示 os.environ                  获取系统环境变量 os.path.abspath(path)       返回path规范化的绝对路径 os.path.split(path)         将path分割成目录和文件名二元组返回 os.path.dirname(path)       返回path的目录。其实就是os.path.split(path)的第一个元素 os.path.basename(path)      返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素 os.path.exists(path)        如果path存在,返回True;如果path不存在,返回False os.path.isabs(path)         如果path是绝对路径,返回True os.path.isfile(path)        如果path是一个存在的文件,返回True。否则返回False os.path.isdir(path)         如果path是一个存在的目录,则返回True。否则返回False os.path.join(path1[, path2[, ...]])  将多个路径组合后返回,第一个绝对路径之前的参数将被忽略 os.path.getatime(path)      返回path所指向的文件或者目录的最后存取时间 os.path.getmtime(path)      返回path所指向的文件或者目录的最后修改时间

Os.popen ("dir"). Read () Get directory information

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.