Python os module, os.path module common method

os module: os module contains common operating system functions in the python, the following are some of the more useful in the os module.

os.sepIt may be substituted operating system specific path separator. The windows as "\"

os.nameString specifies which platform you are using. For example, for Windows, it is 'nt', and for Linux / Unix users, it is 'posix'.

os.getcwd() Function to get the current working directory that the current directory path Python script work.

os.getenv() Gets an environment variable, if none none

os.putenv(key, value) Set an environment variable value

os.listdir(path) Returns all file and directory names in the directory.

os.remove(path) Function is used to delete a file.

os.system(command) Function is used to run shell commands.

os.linesepGiven the current line terminator string platforms. For example, Windows uses '\ r \ n', Linux use '\ n' Mac uses '\ r'.

os.curdir: Returns the current directory ( '.')
os.chdir(dirname): Change the working directory to the dirname


os.getcwd() Gets the current working directory that the current directory path python script work

os.chdir("dirname") Script to change the current working directory; cd at the equivalent of shell

os.curdir Returns the current directory: ( '.')

os.pardir Get the current directory's parent directory string name :( '..')

os.makedirs('dirname1/dirname2') The multilayer recursive directory may be generated

os.removedirs('dirname1') If the directory is empty, delete, and recursively to the parent directory, should also empty, delete, and so on

os.mkdir('dirname') Generating a single level directory; is equivalent to the shell mkdir dirname

os.rmdir('dirname') Delete single stage empty directory, if the directory is not empty can not be deleted, an error; the equivalent of a shell rmdir dirname

os.listdir('dirname') Lists all the files and subdirectories in the specified directory, including hidden files, and print as a list

os.remove() Delete a file

os.rename("oldname","newname") Rename the file / directory

os.stat('path/filename') Get file / directory information

os.sep Output operation system-specific path separator, the win is "\", is the Linux "/"

os.linesep The current output line terminator platform, the win is "\ t \ n", as "\ n" under Linux

os.pathsep Win for the next character string for dividing the output file path;, under Linux is:

os.nameIt indicates the current output string using the internet. win -> 'nt'; Linux -> 'posix'

os.system("bash command") Run shell commands, direct display

os.environ Acquisition system environment variables


os.path common methods:

os.path.abspath(path) Returns the absolute pathname path of standardization

os.path.split(path) The path into the directory and file name of the tuple returned

os.path.dirname(path)Returns the directory path of. In fact, os.path.split (path) of the first element

os.path.basename(path)Returns the path of the last file name. How path to / \ or end, it will return a null value. That second element os.path.split (path) of

os.path.exists(path) If the path exists, return True; if the path does not exist, returns False

os.path.isabs(path) If the path is an absolute path, returns True

os.path.isfile(path)If the path is an existing file and returns True. Otherwise it returns False

os.path.isdir(path)If the path is a directory exists, then return True. Otherwise it returns False

os.path.join(path1[, path2[, ...]]) After combination of a plurality of paths returned, before the first parameter is ignored absolute path

os.path.getatime(path) Returns the last access time path points to a file or directory

os.path.getmtime(path) Returns the file or directory path points to the last modification time

os.path.getsize(path) The size of the return path

os.path.normpath(os.path.join(os.path.abspath(__file__),'..','..','..')) It represents the return on the parent directory of the current file

Guess you like

Origin www.cnblogs.com/wbyixx/p/12196034.html