OS module: a built-in module for getting started with Python


OS module: a built-in module for getting started with Python

1. OS OS is to interact with the operating system and give instructions to the operation

OS module is an interface that interacts with the operating system. Its functions are mostly related to working directory, path, file, etc

(1) working path

Print (OS. Getcwd()) ා get the path of the current file work***
Os.chdir ("D: \ Python? S25 \ day16")? Path switching**
Print (OS. Curdir) ා returns the current directory: ('.')
Print (OS. Pardir) ා get the parent directory string name of the current directory: ('..')

(2) folder

Os.mkdir ("A2")?? create folder
Os.rmdir ("A2")? Delete folder
OS. Makedirs ('a1 / A2 / A3 ') recursively creates folders
Os.removediers ("A1 / A2 / A3") (recursively delete folders
Print (OS. Listdir (R "D: \ Python ﹣ S25 \ Day17") ᦇ view all contents under the current file

(3) documents

OS. Remove (R "D: \ Python ﹣ S25 \ Day17 \ a") ᦇ delete the file completely, and it can't be found
OS. Rename() (rename)

OS. Stat ('path / filename ') - get file / directory information

(4) path

Print (OS. Path. Abspath ("test")) ාreturns the absolute path     
Print (OS. Path. Split (R "D: \ Python ﹣ S25 \ Day17 \ test") 񖓿 split the path into a path and a file name 
Print (OS. Path. Dirname (R "D: \ Python ﹣ S25 \ Day17 \ test")) ා get to the parent directory                   
Print (OS. Path. Basename (R "D: \ Python ﹣ S25 \ Day17 \ test")) ා get the file name  
Print (OS. Path. Join ("D: \ Python", "Day17", "test")) (path splicing (very important)
Os.path.exists (path) - returns true if path exists, false if path does not exist  
Os.path.isabs (path) - returns true if path is absolute  
Os.path.isfile (path) - returns true if path is an existing file. Otherwise, return false 
Os.path.isdir (path) - returns true if path is an existing directory. Otherwise, return false  
Os.path.getatime (path) returns the last access time of the file or directory to which the path refers  
Os.path.getmtime (path) returns the last modification time of the file or directory pointed to by path  
Os.path.getsize (path) ා return the size of path

(5) judgment

Print (OS. Path. Exists (R "D:  
Print (OS. Path. Isabs (R "D: \ Python ﹣ S26 \ Day17 \ blog")) ා determine whether the path is absolute or not  
Print (OS. Path. Isdir (R "D: \ Python ﹣ S25 \ Day17 \ blog")) ා determine whether it is a folder or not 
Print (OS. Path. Isfile (R "D: \ Python ﹣ S25 \ Day17 \ blog")) ා judge whether it is a file or not  

Print (OS. Path. GetSize (R "D: \ Python \ S25 \ Day17 \ 01 today's content. Py")) ා get the file size
Print (OS. Path. GetSize (R "D: \ Python? S25") 񖓿 get file size

(6) operating system related

Os.sep ා output the path separator specific to the operating system, which is "\ \" under win and "/" under Linux 
Os.linesep ා output the line terminator used by the current platform, which is "\ t \ n" under win and "\ n" under Linux 
Os.pathsep ා output the string used to split the file path: win; Linux: 
Os.name ා the output string indicates the current platform. win->'nt'; Linux->'posix' 
#Related to executing system commands
os.system("bash command")  
#Run the shell command to display directly  
os.popen("bash command").read()  
#Run the shell command to get the execution result  
#Get system environment variables

(7) structure description of obtaining file / directory information by os.stat (‘path / filename ‘)

#Stat structure:
St mode: inode protection mode
St? Ino: inode? Node number.
St? Dev: the device where inode resides.
St? Nlink: number of links for inode.
St uid: the user ID of the owner.
St × GID: the owner's group ID.
St Ou size: the size of a normal file in bytes; contains data waiting for some special files.
St ා atime: the time of last visit.
St? Mtime: the time of the last modification.
St? CTime: the "CTime" reported by the operating system. On some systems (such as Unix), it is the time of the latest metadata change, and on other systems (such as windows), it is the time of creation (see the platform's documentation for details)