We learn the basics of OS libraries from the level of the operating system:
1. Type of operating system
2. Execute operating system commands
3, the location of the path
4. Create a Directory
5. Delete Files
6. Delete Directory
7. Change the path
8. Get content under the catalogue
1 , Os.name functions
function: Gets the operating system currently in use (not enough information to get detailed)
where ' NT ' is windows, ' POSIX ' is Linux or Unix
>>> Import OS
>>> System_type=os.name
>>> if system_type== ' NT ':
Print (' SystemType is windows! ')
Elif system_type== ' POSIX ':
Print (' SystemType is Linux or unix! ')
Else
Print (' SystemType is other! ')
After running, the output is as follows:
System type is windows!
2 , Os.system () function
function: Run shell command
>>> Import OS
>>> os.system (' dir ')
3 , OS.GETCWD () function
function: Get the current directory, Python's work
>>> Import OS
>>> PWD=OS.GETCWD ()
>>> Print (PWD)
After running, the output is as follows:
D:\software\12.python
4 , Os.mkdir () function
function: Create a new directory
eg : Create a test directory under the system D drive
>>> Import OS
>>> os.mkdir (' d:\\test ')
5 , Os.removedirs () function
Function: Delete the specified directory
eg : Delete Test directory under D drive
>>> Import OS
>>> os.removedirs (' d:\\test ')
6 , Os.remove () function
Function: Delete the specified file
eg : delete file.txt file
>>>os.chdir (' D:\\test ')
>>>OS.GETCWD ()
' D:\\test '
>>>os.remove (' file.txt ')
Note: This function can only delete files in the current directory
7 , Os.chdir () function
function: Change the current path to the specified path
eg : I now go from the current path to the path specified by filepath
>>> Import OS
>>> PWD=OS.GETCWD ()
>>> Print (PWD)
D:\test
>>> filepath= ' D:\\download '
>>> Os.chdir (filepath)
>>> PWD=OS.GETCWD ()
>>> Print (PWD)
D:\download
8 , Os.listdir () function
function: Returns all directories and files under the specified directory
eg : Lists all files and directories in the current directory
>>>import OS
>>>PWD=OS.GETCWD ()
>>>print (PWD)
D:\download
This article from "Technology bo" blog, declined reprint!
Python series------function type--os function library