Python non-return _os and System module

Source: Internet
Author: User
Tags exit in stdin

The OS module is simply a python system-programmed operating module that can handle files and directories that we manually need to do daily, and the OS module provides an interface for invoking the operating system.

Several important functions:

OS.GETCWD () #查看当前所在路径, the default is the Python program's Path Os.chdir () #切换目录, the function is the same as the CD command in Linux os.curdir# return the current directory '. ' os.pardir# returns the current parent directory ' ... ' Os.makedirs () #新建多级目录, the function is the same as the Mkdir-p command in Linux os.removedirs () #删除多级目录, if the directory is not empty, cannot be deleted, and so on Os.mkdir () #新建单级目录os. RmDir () # Delete the single-level catalog Os.listdir () #列出制定目录下所有的目录和文件, put back os.remove () #删除文件os. Rename () #重命名文件os. Stat () #获取目录和文件信息os. sep# Output current operating system delimiter, Windows is ' \ \ ', Linux is '/' os.linesep# output current operating system line terminator, Windows is ' \t\n ', Linux is ' \ n ' os.pathsep# Output the string used to split the file path os.name# output is currently using the platform, Windows is ' NT ', Linux is ' Postix ' Os.system () #输出Linux命令os. Environ () # Gets the system environment variable Os.path.abspath () #输出绝对路径os. Path.split () #将path分割成目录和文件名的二元组返回os. Path.dirname () # Returns the directory of Path Os.path.basename () #返回path最后的文件名os. Path.exists () #判断路径是否存在, returning Ture or Falseos.path.isabs () #判断路径是否绝对路径, Returns TRUE or Falseos.path.isflie () #判读是否是文件, returns True or Falseos.path.isdir () #判断是否是目录, returns True or Falseos.path.join () # Returns multiple file combinations to Os.path.getatime () #获取路径最后存取时间os. Path.getmtime () #获取路径最后修改时间

Let's look at an example of each function:

1.OS.GETCWD (), Os.chdir ()

Import osos.getcwd () #当前所在目录os. ChDir (R'd:\\') #切换路径 os.getcwd ()

Output Result:

' D:\\Program files\\python3.6 ' ' d:\\ '

2.os.curdir and Os.pardir

Os.curdiros.pardir

Output Result:

' . ' ' .. '

3.os.makedirs ()

Create a new multilevel directory, similar to the role of Mkdir-p in Linux.

Import osos.path.exists ('d:\\234') #检查d: \\234 directory exists os.makedirs (' d:\\234\\234 ' ) #新建多级目录 d:\\234\\234
Os.path.exists (' d:\\234 ') #检查d: \\234 directory exists os.path.exists ('d:\\234\\234') # Check if the d:\\234\\234 directory exists

Output Result:

False #d: \\234 directory does not exist true #d: \\234 directory exists true #d: \\234\\234 directory exists

The Os.makedirs () function is used to create a new d:\\234 and d:\\234\234 two directories, and the two directories are parent-child relationships.

4.os.removedirs ()

Import OS Print (Os.path.exists ('d:\\234')) Print (Os.path.exists ('d:\\234\\234')) #判断d: \\234\\234 directory exists os.removedirs ( ' d:\\234\\234 ' ) #删除d: \\234 all, including subdirectories \\234print(os.path.exists ('d:\\234' )) #判断是否已经删除子目录 \\234

Output Result:

Truetruefalse

From the output of os.path.exists (' d:\\234 '), it is shown that os.removedirs () has deleted both d:\\234 and subdirectories \\234.

5.os.mkdir and Os.rmdir

Os.mkdir and Os.rmdir usage, like mkdir, RmDir in Linux, create and delete directories.

ImportOSPrint(Os.path.exists ('d:\\234')) Os.mkdir ('d:\\234')Print(Os.path.exists ('d:\\234')) Os.rmdir ('d:\\234')Print(Os.path.exists ('d:\\234'))

Output Result:

Falsetruefalse

6.os.listdir

Import Osprint (Os.listdir (' d:\\ '))

Output Result:

['$RECYCLE. BIN','360 Secure Browser Download','Baidunetdiskdownload','dameware7.5.5','MSOCache','office_2013_x64','Program Files','Program Files (x86)','python','System Volume Information','Users']

The Os.listdir () function returns the result as a list, outputting all the directories under the path strength.

7.os.remove and Os.rename

Os.remove () Delete file, Os.rename (oldname,newname) Rename file

Import OS
Print (os.path.exists (' D:\\python.txt '))
Os.rename (' D:\\python.txt ', ' d:\\python123.txt ')
Print (os.path.exists (' D:\\python123.txt '))
Print (os.path.exists (' D:\\python.txt '))
Os.remove (' D:\\python123.txt ')
Print (os.path.exists (' D:\\python123.txt '))
Truetrue  #os. Rename () Python.txt the same name as Python123.txtfalse #python. txt has been named Python123.txtfalse #python已被os. Remove ( ) Delete

8.os.stat ()

Import OS Print (Os.stat ('d:\\'))

Output Result:

Os.stat_result (st_mode=16895, st_ino=1407374883553285, st_dev=731043, St_nlink=1, St_uid=0, St_gid=0, st_size=4096, st_atime=1505912747, st_mtime=1505912747, st_ctime=1505665043)

9.os.system ()

Os.system () outputs the Linux command.

10.os.path.abspath ()

Os.path.abspath () Output absolute path

Import OS
Print (OS.GETCWD ())
Print (Os.path.abspath (' 234.txt '))
Os.chdir (' d:\\ ')
Print (Os.path.abspath (' 234.txt '))

Output Result:

E:\Len_Pyt #当前路径E: \len_pyt\234.txt #输出绝对路径d: \234.txt #修改d: \ \ After output absolute path

11.os.path.split (), Os.path.dirname (), Os.path.basename ()

Os.path.split () returns a two-tuple that splits the path into a directory and a file name

Os.path.dirname () returns the directory of path

Os.path.basename () returns the last file name of path

12.os.path.exists (), Os.path.isfile (), Os.path.isdir (), Os.path.isabs ()

Determine whether the path exists, whether it is a file, whether it is a directory, whether it is absolute, and returns a Boolean value.

13.os.path.getatime () #获取路径最后存取时间

Os.path.getmtime () #获取路径最后修改时间

SYS module:

SYS.ARGV #获取脚本名后的参数sys. Exit (n) #调用 sys.exit(n) can exit the program halfway, and when the argument is not 0 o'clock, an exception is thrown SystemExit , allowing the exception to be caught in the main program. Sys.path () #获取所有python路径sys. Path.insert () #插入python模块sys. Platfrom () #获取当前平台
    • sys.argv: Implements passing parameters to the program from outside the program.

    • sys.exit([arg]): Exit in the middle of the program, arg=0 for normal exit.

    • sys.getdefaultencoding(): Gets the current encoding of the system, which is generally ASCII by default.

    • sys.setdefaultencoding(): Set system default encoding, do not see this method when executing dir (SYS), do not pass in interpreter, can execute reload (SYS), execute setdefaultencoding (' UTF8 ') at this time, set system default encoding to UTF8. (See Set system default encoding)

    • sys.getfilesystemencoding(): Get file system using encoding method, return ' MBCS ' under Windows, Mac return ' Utf-8 '.

    • sys.path: Gets a collection of strings for the specified module's search path, which can be placed in a given path, and can be found correctly when import is in the program.

    • sys.platform: Gets the current system platform.

    • sys.stdin,sys.stdout,sys.stderr: stdin, stdout, and stderr variables contain stream objects that correspond to standard I/O streams. If you need more control over the output, and print does not meet your requirements, they are what you need. You can also replace them, so you can redirect output and input to other devices, or handle them in a non-standard way

  

Python non-return _os and System 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.