When Python is programmed, it often deals with files and directories, which is not the OS module. The OS module contains common operating system features, regardless of the specific platform. The following list of commonly used commands
1. Os.name ()--judging the platform that is now being applied, Windows returns ' NT '; Linux returns ' POSIX '
2. OS.GETCWD ()--Get the current work directory.
3. Os.listdir ()--Specify all files and directory names in all directories. Cases:
Listed in the form of a list, which does not distinguish between directories and files.
4. Os.remove ()--delete the specified file
5. Os.rmdir ()--delete the specified directory
6. Os.mkdir ()--Create a directory
Note: This can only be done by building a layer, to make it available for recursion:os.makedirs ()
7. Os.path.isfile ()--Determines whether the specified object is a file. is return true, otherwise false
8. Os.path.isdir ()--Determines whether the specified object is a directory. is true, otherwise false. Example: 9. os.path.exists ()--verifies that the specified object exists. is true, otherwise false. Example:
os.path.split ()--Returns the directory and file name of the path. Cases:
This is just the two parts of the front and back apart. is to find the last one '/'. See Example:
OS.GETCWD ()--Get the current working directory (get present work dir)
Os.system ()-Executes the shell command. Cases:
Note : When you run the shell command here, if you want to invoke a variable before python, you can do so in the following way:
Var=123os. environ [' var ']=str (Var)//Note here [] is "string" Os.system (' echo $var ')
Os.chdir ()--Change directory to the specified directory
os.path.getsize ()--Gets the size of the file, if it is a directory, returns 0
Os.path.abspath ()--Get the absolute path. Cases:
os.path.join (path, name)--Connection directory and file name. Cases:
17.os.path.basename (path)--Return file name
os.path.dirname (path)--Return file path
19. Get the actual directory where the program is located
Import Osimport sysif __name__ = = "__main__": print Os.path.realpath (sys.argv[0]) print os.path.split ( Os.path.realpath (sys.argv[0]) print os.path.split (Os.path.realpath (sys.argv[0])) [0]
Execution results
| 123 |
/home/jihite/ftp/del.py(‘/home/jihite/ftp‘, ‘del.py‘)/home/jihite/ftp |
detail --os.path.spilit () separate directories and file areas
| 12345 |
>>> import os>>> os.path.split("a/b/c/d")(‘a/b/c‘, ‘d‘)>>> os.path.split("a/b/c/d/")(‘a/b/c/d‘, ‘‘) |
Python OS module common commands