Python OS Module--Paths, files, system commands, etc.

Source: Internet
Author: User
Tags posix

The OS module contains common operating system features.

Note: The function parameter path is a file or directory, filename is the path to the file, DirName is the path to the directory, the path can be a relative path, or an absolute path

Common or important functions are bold fonts

OS module:

Os.name returns the current operating system name (' POSIX ', ' nt ', ' Os2 ', ' mac ', ' CE ', ' riscos '), Windows ' NT ', Linux for ' POSIX '

OS.SEP = = Os.path.sep Returns the delimiter between the parts of the path, Linux is '/', Windows is ' \ \ ', because ' \ ' is escaped in Python, so the return value is two ' \ '

OS.EXTSEP returns the delimiter between the filename and the file name extension: '. '

Os.pathsep directory delimiter, ': '

Os.linesep line break delimiter, Linux is ' \ n ', Windows is ' \ r \ n '

os.listdir (path) lists all files and subdirectories under the directory

os.getcwd () Gets the current working directory path

Os.mkdir (dirname) Create directory, directory already exists will no longer be created

Os.makedirs (dirname) to create a directory recursively, the parent directory does not exist, will be created, directory already exists will no longer be created

Os.remove (filename) deletes the specified file

Os.rmdir (dirname) Delete an empty directory

Os.removedirs (dirname) recursively delete empty directories, such as Os.removedirs (' Dir1/dir2/dir3 '), Dir1 and others except Dir2, Dir2 Dir3 and Dir3

Os.rename (SRC, DST) renaming a file or directory

Os.renames (SRC, DST) renaming a file or directory recursively

os.walk () http://www.runoob.com/python/os-walk.html

Process-related operations in the OS:

os.system (CMD) runs a program or command and returns immediately, and many of these operations can be done with the function

Startfile (filename) runs with the program associated with the file, and immediately returns when the associated program is opened

EXECL (PATH) runs a program to replace the current process, which can be blocked

_exit (n) Exit program

Os.path module:

os.path.isfile (PATH) verifies whether the given path is a file that returns a bool value

os.path.isdir (PATH) verifies whether the given path is a directory and returns a bool value

os.path.exists (PATH) verifies that the given path exists and returns a bool value

os.path.getsize (path) Gets the size of the path (file or directory), and if it is a directory, returns 0L, in bytes

Os.path.abspath (Path) obtains an absolute path

Os.path.normpath (path) specification path string form, result/change//

os.path.split (path) splits the path name and file name, the return value is tuple: (pathname, filename. file name extension), if the path is a directory, then return: (Pathname, ')

os.path.splitext (path) detaches the file name and extension, returns the value format: (file name, extension), and returns if the parameter is a directory: (Path, ')

Os.path.join (path,name) connection directory with file name or directory, the return value is: Path/name

os.path.basename (PATH) returns the file name, actually splits the last "/" of the path, returns the latter, and the directory Also

os.path.dirname (PATH) returns the file path, actually dividing the last "/" of the path, returning the former, and the directory as well

The last access time of the Os.path.getatime (path) file or folder, from the new era to the number of seconds it was accessed

The last modified time of the Os.path.getmtime (path) file or folder

Os.path.getctime (path) file or folder creation time

 

Python OS Module--Paths, files, system commands, etc.

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.