Python OS module

Source: Internet
Author: User

OS module provides an interface for invoking the operating system

1. Get the current path

>>> Import OS

>>> OS.GETCWD ()//equivalent to Linux command pwd

'/root '


2. Switch directories

>>> Os.chdir ("/usr/local")//equivalent to Linux command CD

>>> OS.GETCWD ()

'/usr/local '


3. Create a directory recursively

>>> os.makedirs ("/a/b/c")//makedirs can achieve recursive creation of the directory function

>>> Os.chdir ("/a/b/c")

>>> OS.GETCWD ()

'/a/b/c '

>>> Os.mkdir ("/a")//mkdir cannot be created recursively, only create directories on existing basis


4. Delete Directory

>>> os.removedirs ("/a/b/c")//Note: If the directory is empty, delete it, and recursively to the previous level directory, if the previous directory is empty, also delete


>>> Os.rmdir ("/a/b/c")//rmdir will only delete the C directory


5. List the contents of a directory

>>> os.listdir ("/tmp")//parentheses support the use of "." or ".." to represent the current directory and the parent directory

['. Ice-unix ', ' pulse-fs462evlc7dh ', ' keyring-uwyk0a ', '. esd-0 ', '. X0-lock ', ' pulse-jkptspm9pkfd ', ' keyring-mwnb3a ', ' ORBIT-GDM ', ' virtual-root.28g7kt ', '. X11-unix ', ' keyring-vskfbk ', ' keyring-s0txnr ', ' virtual-root. T44xz0 ']


6. Delete Files

>>> os.remove ("/root/test.txt")//The parameter in parentheses is the file path


7. renaming files or directories

>>> Os.rename ("/A", "/C")//parentheses with the parameter "old", "new"


8. Get information about a file or directory

>>> os.stat ("/root/test.py")//Print all properties by default

Os.stat_result (st_mode=33188, st_ino=1198015, st_dev=64768, St_nlink=1, St_uid=0, St_gid=0, st_size=261, st_atime= 1521601271, st_mtime=1521601271, st_ctime=1521601271)

>>> os.stat ("/root/test.py"). St_size//can also take one of these values

261


9, the output operating system path delimiter, such as: windows for "\ \", Linux "/"

>>> Os.sep

'/'


10, output the operating system line break delimiter, for example: Windows is "\ r \ n", Linux "\ n"

>>> Os.linesep

' \ n '


11. View the environment variables of the system

>>> Os.environ

Too much content, no more copying


12. Get the delimiter of the current operating system environment variable, for example: Windows is ";", Linux is ":"

>>> Os.pathsep

':'


13. Display the current system

>>> Os.name

' POSIX '


14. Command to execute operating system

>>> Os.system ("Ls-l")

Total dosage 21840

-RW-------. 1 root root 1612 February 7 17:21 anaconda-ks.cfg

-rw-r--r--. 1 root root 46478 February 7 17:21 Install.log

-rw-r--r--. 1 root root 10033 February 7 17:19 Install.log.syslog

-rw-r--r--. 1 root root 22256403 December python-3.6.0.tgz


15, the file path and file segmentation

>>> os.path.split ("/a/b/c/a.txt")//split the whole path of a file

('/a/b/c ', ' a.txt ')


16. Get Directory Name

>>> os.path.dirname ("/a/b/c/a.txt")

'/a/b/c '


17. Get the file name

>>> os.path.basename ("/a/b/c/a.txt")

' A.txt '


18, determine whether the path exists, you can determine the directory and file

>>> os.path.exists ("/b")

False

>>> os.path.exists ("/C")

True

>>> os.path.exists ("/etc/passwd")

True


19. Determine if the path is an absolute path

>>> os.path.isabs ("/etc/passwd")

True

>>> os.path.isabs ("passwd")

False


20. Determine if the parameter is a file

>>> os.path.isfile ("/etc/passwd")

True

>>> os.path.isfile ("/etc")

False


21. Stitching Path

>>> os.path.join ("/root", "passwd", "Passwd.txt")

'/root/passwd/passwd.txt '


22. Get the file or directory access time, return timestamp

>>> os.path.getatime ("/etc/passwd")

1521600541.5160062

>>> os.path.getatime ("/etc")

1521531717.0806012


23. Get the modified time of the file or directory

>>> os.path.getmtime ("/etc")

1521600007.5530176

>>> os.path.getmtime ("/etc/passwd")

1517995284.6799996


24, return the absolute path of the file

Os.path.abspath (__file__)//return absolute path to file
Print (Os.path.dirname (Os.path.abspath (__file__)))//return to the directory where the file is located


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.