OS module: interface of the operating system
Source code in lib/os.py
- os.name determine the system Os.system () Run the shell command
- ' nt ': Windows;
- ' POSIX ': ' Linux/unix '.
- os.chdir (path)
- os.fchdir (FD)
- os.getcwd
- os.fsencode (filename)
- os.fsdecode (filename)
- os.getenv (key, Default=none) Gets the environment variable, returns the value of the environment variable key if it exists, or returns none if there is no (or default value set)
- OS.GETENVB (key, Default=none) ibid., result is byte
- os.getlogin returns the user name of the machine login
>>> os.getlogin () ' Frange '
-
Os.getpid () Current process ID
-
Os.getppid () Current parent process ID
-
OS.GETPGRP () current process group ID available: UNIX
-
Os.putenv (key, value) sets the environment variable for key to value
-
Os.unsetenv (Key) de-set (delete) environment variable named key
File Related
-
Os.listdir (path) lists the folders or files below the path, and returns the list
-
Os.mkdir (path,mode=0o777, *, Dir_fd=none) creates a directory named path, with the permission mode, and an error if the directory exists
-
Os.makedirs (name, mode=0o777, Exist_ok=false) recursively creates a function of the directory. Similar to mkdir (), but produces all intermediate directories and leaf directories. Exist_ok=false, if the target directory exists, the error will be
-
Os.remove (path) Delete file path, if the directory is the error
-
Os.removedirs (name) to delete a directory recursively
-
Os.rename (SRC,DST) renames the file or directory src to DST
-
Os.renames (old, new) renaming function recursively processing directory files
-
Os.replace (SRC,DST) renaming a file or directory to DST
-
Os.rmdir (path) Delete directory path
-
Os.scandir (path= '. ') returns an iterator to the Direntry object that corresponds to the directory entry given by path. These entries are generated in any order, with special entries '. ' and ' ... ' Not included.
-
Os.stat (Path, *, Dir_fd=none, follow_symlinks=true) Gets the state of the file or file descriptor. Performs the same stat () system call on the given path
>>> a = Os.stat (' 1.txt ') >>> aos.stat_result (st_mode=33206, st_ino=844424930345609, st_dev= 2423465123, St_nlink=1, St_uid=0, St_gid=0, st_size=4, st_atime=1528970669, st_mtime=1528970669, st_ctime=1528970669) >>> A.st_size4
Note:Properties:
St_mode file Mode: File type and file mode bit (permissions).
St_ino inode node number.
St_dev The identifier of the device where the file resides.
St_nlink the number of hard links.
The user identifier of the St_uid file owner.
The group identifier for the St_gid file owner.
The size of the st_size file, in bytes, if it is a regular file or symbolic link. The size of the symbolic link is the length of the pathname it contains, not the null byte that is terminated.
Time stamp:
St_atime the time of the most recent visit is expressed in seconds.
St_mtime The most recent content modification time is expressed in seconds.
St_ctime depends on the platform:
The latest metadata change time on UNIX, the time that is created on windows, expressed in seconds. St_atime_ns the time of the most recent visit is expressed in nanoseconds, expressed as an integer.
St_mtime_ns the time of the latest content modification is expressed in nanoseconds, expressed as an integer.
St_ctime_ns depends on the platform:
The most recent metadata change time on UNIX, the time created on Windows, expressed in nanoseconds as an integer.
- Os.walk (Top, Topdown=true, Onerror=none, Followlinks=false) traverse the directory tree, top-down or bottom-up to generate the file name under the directory tree: For each directory in the root top (including the root top itself), it yields a 3-tuple (Dirpath, dirnames, filenames).
- Dirpath is a string that is the directory path
- Dirnames is a list of names of Dirpath sub-directories (not including '. ') and ' ... ').
- Filenames is a list of the names of Dirpath non-directory files.
-
Os.fwalk (top= '. ', Topdown=true, Onerror=none, *, Follow_symlinks=false, dir_fd=none) behaves very similar to walk (), unlike it produces a 4-tuple ( Dirpath, Dirnames, filenames, DIRFD), and support DIR_FD.
-
Os.fork () leaves a child process and returns child and parent processes (using UNIX)
-
The Os.plock (OP) locks the program segment into memory. The value of the OP determines which segments are locked
-
Os.wait () Waits for the child process to complete and returns a tuple that contains its PID and exit status indication: A 16-bit number whose low byte is the signal number that kills the process, its high byte is the exit state (if the signal number is 0), or if the core file is generated, a low byte high is set.
-
Os.cpu_count () returns the number of system CPUs, or none if unsure
-
Os.sep the word that the system uses to style the path name component
-
Os.startfile (path) to open files with related applications on your PC
Python OS module rollup