Home >Backend Development >Python Tutorial >Summary of python os module functions and methods

Summary of python os module functions and methods

高洛峰
高洛峰Original
2016-10-20 09:18:541347browse

os.sep can replace operating system specific path separators

os.linesep The string gives the line terminator used by the current platform. For example, Windows uses 'rn', Linux uses 'n' and Mac uses 'r'.

os.name String indicating the platform you are using. For example, for Windows, it is 'nt', and for Linux/Unix users, it is 'posix'

os.getcwd() function gets the current working directory,

os.getenv() and os.putenv() functions respectively Used to read and set environment variables.

os.listdir(dirname): List the directories and files under dirname

os.remove() function is used to delete a file.

os.curdir: Return to the previous directory ('.')

os.chdir(dirname): Change the working directory to dirname

getatime(path): The last access time of the file or folder, from the new epoch to the time of access The number of seconds

getmtime(path): the last modification time of the file or folder

getctime(path): the creation time of the file or folder


os.path module:

os.path.isfile( ) and os.path.isdir() functions respectively check whether the given path is a file or a directory, and return bool value

os.path.exists() function is used to check whether the given path really exists and returns bool

os.path.getsize(name): Get the file size. If name is a directory, return 0L. Return long. The unit is bytes. os.path.abspath(name): Get the absolute path. os.path.normpath(path): Standardize the path string form, and the result is usually / becomes //,

os.path.split(name): split the name into a path name and a file name, the result is (path name, file name. file extension) (In fact, if you use directories at all, it will also split the last directory as the file name, and it will not determine whether the file or directory exists)

os.path.splitext(filename): Split the file name and extension The name result is (filename, extension) If the parameter is a path, it returns (path, '')

os.path.join(path,name): Connect the directory and the file name or the directory result is path/name

os .path.basename(path): The file name returned is actually divided by the last "/" of the path and the latter is returned. Regardless of whether the parameter is a path or a file, it is the same as os.path.split(name). The difference is that the latter returns two value tuples

os.path.dirname(path): The return file path is actually the path. The last "/" split returns the former. Regardless of whether the parameter is a path or a file, the os.system() function is used to run shell commands. The above are only the common ones. List them all below: The os module wraps the functions of different operating systems. The universal interface allows users to use the same function interface and return the same structure of results under different operating systems.


os.name: Returns the current operating system name ('posix', 'nt', 'os2', 'mac', 'ce' or 'riscos')

os defines a set of files and paths for different operations Expression parameters in the system, such as

os.sep (folder separator, in windows)

os.extsep (extension separator, in windows. )

os.pathsep (directory separator, in windows in; )

os.linesep (newline separator, rn in windows)


os has a large number of related functions for file and path operations, such as:

listdir(path): List all files in the directory

makedir(path): Create a folder. Note: Creating an existing folder will cause an exception.

makedirs(path): Create a folder recursively. Note: Creating an existing folder will cause an exception.

remove(filename): Delete a file

rmdir(path): Delete a folder. Note: Deleting a non-empty folder will cause an exception.

removedirs(path): Delete the folder recursively until one level of folder is non-empty. Note: File The folder path cannot end with ''

rename(src,dst): Rename the file or folder (the path can be changed, but the target file cannot be overwritten)

renames(src,dst): Recursively give the file or file name Rename

walk(path): List all files and folders under path

Process-related operations in os, such as:

execl(path): Run a program to replace the current process, which will block the operation

_exit(n): Exit the program

startfile(filename): Run with the program associated with the file. After the associated program is opened, it will return immediately

system(cmd): Run a program or command, it will return immediately and execute in cmd After completion, the cmd exit code will be returned

os.path: Calls different modules in different operating systems. It is an importable module. This module provides many useful operations:

abspath(path): Returns path The absolute path. If path is already an absolute path, it will be maintained.

basename(path): Returns the file name in path.

commonprefix(list): Returns the unified prefix in the list, used to obtain the same content from the left of a set of strings

dirname(path): Returns the folder part of the path, the result does not contain ''

exists (path): Whether the file or folder exists

getatime(path): The last access time of the file or folder, the number of seconds from the new epoch to the time of access

getmtime(path): The last modification time of the file or folder

getctime(path): The creation time of the file or folder

getsize(path): The size of the file or folder, if it is a folder, return 0

isabs(path): Return whether it is an absolute path

isfile(path) :Returns whether it is a file path

isdir(path): Returns whether it is a folder path

islink(path): Returns whether it is a shortcut

join(path1, path2,...): Combine paths, if If there is an absolute path, the previous path will be deleted

normcase(path): Convert the separator in the path

normpath(path): Convert the path to a path recognized by the system

realpath(path): Convert the path to Absolute path

split(path): Split the path into (folder, file name)

splitext(path): Split the path into (the rest, .extension), if there is no extension in the file name, the extension Part of it is an empty string

When operating on an object that is not supported by the system, an OSError exception is thrown.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn