OS module, pythonos Module

Source: Internet
Author: User

OS module, pythonos Module

I. Introduction

The OS module in the Python standard library contains common operating system functions. This module is especially important if you want your program to be unrelated to the platform. That is, it allows a program to run in Linux and Windows without any changes or any problems.

II. Specific modules

2.1 OS. getcwd (): Get the current working directory

# Windows >>> OS. getcwd () 'G: \ python' #\\ escape path separator \# Linux >>>> OS. getcwd () '/root/monitor'

2.2 OS. chdir ("dirname"): change the current working directory

# Windows> OS. chdir (r "G: \ python \ untitled") # r escape >>> OS. getcwd () 'G: \ python \ untitled '# Linux >>> OS. chdir ('/usr/local') >>> OS. getcwd () '/usr/local'

2.3 OS. curdir: returns the current directory ,'.'

2.4 OS. pardir: returns the parent directory of the current directory ,'..'

>>> import os>>> os.curdir'.'>>> os.pardir'..'

 

2.5 OS. makedirs ('dirname1/dirname2'): generate a multi-layer recursive directory

os.makedirs(r'G:\a\b\c')

  

2.6 OS. mkdir ('dirname'): Only one directory can be created

2.7 OS. removedirs ('dirname1/dirname2'): If the directory is empty, delete it and return it to the previous layer. If the directory is empty, delete it, and so on.

 os.removedirs(r'G:\a\b\c')

 

2.8 OS. rmdir ('dirname'): deletes a single-stage empty directory. If the directory is not empty, it cannot be deleted.

2.9 OS. listdir ('dirname'): lists the files and directories under a specified directory, including hidden files, in the form of a list

>>> os.listdir(r'G:\python')['install', 'program', 'pychart', 'scripts', 'untitled']

  

2.10 OS. remove (): delete an object

2.11 OS. rename ('oldname', 'newname'): rename a file/directory

2.12 OS. stat ('path/filename'): Get File Information

2.13 OS. sep: The Path delimiter in the output operating system, win lower bit '\', linux lower bit '/'

2.14 OS. linesep: The line terminator of the operating system. It must be '/ r/N' and '\ n' of the linux operating system'

2.15 OS. pathsep: output the string used to split the file path

2.16 OS. environ: environment variable in dictionary form

2.17 OS. system: Run shell

2.18 OS. path

# OS. path. abspath (path) the absolute path of the current file >>> OS. path. abspath ('000000') 'G: \ python \ scripts \ 000000' # OS. path. split (path): divides a path into two groups: Directory and file name> OS. path. split (r 'G: \ python \ scripts \ 111 ') ('G: \ python \ scripts', '123') # OS. path. dirname (path) returns the path Directory, which is actually the OS. path. the first split element >>> OS. path. dirname (r 'G: \ python \ scripts \ 111 ') 'G: \ python \ scripts' # OS. path. basename (path) returns the final file name of the path. If the path ends with a slash (/) or slash (\), a null value is returned. That is, OS. path. the second element of split (path) >>> OS. path. basename (r 'G: \ python \ scripts \ 111 ') '000000' # OS. path. exists (path) returns True if path exists. If path does not exist, returns False # OS. path. isabs (path) returns True # OS if path is an absolute path. path. isfile (path) returns True if path is an existing file. Otherwise, False # OS. path. isdir (path) is returned. If path is an existing Directory, True is returned. Otherwise, False # OS. path. join (path1 [, path2 [,...]) returns the result after combining multiple paths. The parameters before the first absolute path are ignored # OS. path. getatime (path) returns the last access time of the file or directory pointed to by path # OS. path. getmtime (path) returns the last modification time of the file or directory pointed to by path.

  

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.