OS Module Common Commands
python The standard library for os modules contain common operating system features. This module is especially important if you want your program to be platform-agnostic. That is, it allows a program to be written without any changes, and without any problems, in the linux windows run.
Below are some of the os The more useful part of the module. Most of them are simple and straightforward.
Os.name The string indicates the platform you are using. For example, for Windows, it's ' NT ', and for Linux/unix users, it's ' POSIX ' .
OS.GETCWD () function to get the current working directory, which is the current Python the directory path where the script works.
os.getenv () and the os.putenv () The function is used to read and set environment variables respectively.
Os.listdir () returns all file and directory names under the specified directory.
>>> Os.listdir (OS.GETCWD ())
[' Django ', ' DLLs ', ' Doc ', ' include ', ' Lib ', ' Libs ', ' LICENSE.txt ',' mysql-python-wininst.log ', ' NEWS.txt ', ' Pil-wininst.log ', "Python.exe ', ' pythonw.exe ', ' README.txt ', ' Removemysql-python.exe ', ' RemovePIL.exe ', ' Removesetuptools.exe ', ' Scripts ', ' setuptools-wininst.log ', ' tcl ', ' Tools ', ' W9xpopen.exe ']
os.remove () The function is used to delete a file.
Os.system () function is used to run shell command, equivalent to windows cmd The command entered in the window.
If it is system words are linux Span style= "font-family:arial" >
> >> os.system (' dir ')
0
> >> os.system (' cmd ') # start dos
Os.sep returns a delimiter that can replace an operating system-specific path divider.
the os.linesep string gives the line terminator used by the current platform. For example,Windows uses ' \ r \ n ',Linux uses ' \ n ' and Mac Use ' \ r ' .
>>> os.linesep
' \ r \ n ' #Windows use ' \ r \ n',Linux uses ' \ n ' and Mac Use ' \ r ' .
>>> Os.sep
' \ \ ' #Windows
Os.listdir (dirname) : Lists dirname directories and files under the
os.curdir: Returns the current directory ---('. ')
Os.chdir (dirname): Changing the working directory to dirname
Python OS Module