Brief introduction
- The OS module is primarily used to provide high-level operation of the system.
Common methods
Os.access (path, mode) # Verify permission Mode Os.chdir (path) # Change current working directory Os.chflags (path, Flags) # Set the path The mark of the path is a number marker. Os.chmod (path, mode) # Change Permissions os.chown (path, UID, GID) # change file owner Os.chroot (PATH) # Change the root of the current process Record Os.close (FD) # Close file descriptor Fdos.closerange (Fd_low, Fd_high) # Close all file descriptors, from Fd_low (included) to Fd_high (not included), error Will ignore Os.curdir # to return to the current directory: ('. ') ) Os.dup (FD) # Copy file descriptor Fdos.dup2 (FD, FD2) # Copy one file descriptor fd to another Fd2os.environ # Get System environment variable Os.fchdir (FD) # Change the current working directory by file descriptor Os.fchmod (FD, mode) # Change the access rights of a file, the file is determined by the parameter FD Specifies that the parameter mode is a file access permission under UNIX. Os.fchown (FD, UID, GID) # Modifies the ownership of a file, which modifies the user ID and user group ID of a file, specified by the file descriptor FD. Os.fdatasync (FD) # Forces a file to be written to disk, specified by the file descriptor FD, but does not force the update of the file's state information. Os.fdopen (fd[, mode[, BufSize]) # Creates a file object from the file descriptor FD and returns the file Object Os.fpathconf (FD, name) # Returns the system configuration information for an open file. Name is the value of the system configuration retrieved, and it may be a string that defines the system values, which areMultiple criteria are specified (posix.1, UNIX, UNIX 98, and others). Os.fstat (FD) # Returns the status of the file descriptor FD, like stat (). OS.FSTATVFS (FD) # Returns information about the file system that contains file descriptor FD files, such as STATVFS () Os.fsync (FD) # Forces a file with file descriptor FD to be written to the hard disk. Os.ftruncate (fd, length) # Crops file descriptor fd corresponding file, so it cannot exceed the file size maximum. OS.GETCWD () # Returns the current working directory Os.getcwdu () # Returns a Unicode object for the current working directory Os.isatty (FD) # returns True if the file descriptor FD is open while connected to the TTY (-like) device, otherwise false. Os.lchflags (path, flags) # Sets the path marked as a number tag, similar to Chflags (), but no soft link os.lchmod (path, mode) # Modify the Connection file permissions Os.lchown (PAT H, UID, GID) # Changes the file owner, similar to Chown, but does not track links. Os.link (SRC, DST) # Creates a hard link, named DST, to the parameter Srcos.listdir (path) # Returns a list of the names of the files or folders contained in the folder specified by path. Os.lseek (FD, POS, how) # Set file descriptor fd Current position for POS, how to modify: Seek_set or 0 sets the computed POS starting from the file; Seek_cur or 1 is calculated from the current position; Os. Seek_end or 2 starts at the end of the file. Valid Os.lstat in Unix,windows (path) # like stat (), but no soft link Os.linesep # The line terminator used by the current platform, win underFor "\t\n", "\ n" under Linux os.major # extracts the device major number from the original device number (using St_dev in stat or St_rdev field). Os.makedev (Major, minor) # A primitive device number with major and minor device number Os.makedirs (path[, mode]) # Recursive folder creation function. Like MkDir (), but all Intermediate-level folders that are created need to contain subfolders. Os.minor # Extracts the device minor number from the original device number (using St_dev in stat or St_rdev field). Os.mkdir (path[, mode]) # Create a folder named path in the digital mode mode. The default mode is 0777 (octal). Os.mkfifo (path[, mode]) # Create a named pipe, mode is a number, default is 0666 (octal) Os.mknod (filename[, mode=0600, device]) # Create a filename System node (file, device special file or name pipe). Os.open (file, flags[, mode]) # Opens a file and sets the desired open option, the mode parameter is optional os.openpty () # Opens a new pseudo-terminal pair. Returns the file descriptor for the Pty and TTY. os.pathconf (path, name) # returns system configuration information for the related file. OS.PATHSEP # string used to split the file path Os.pardir # Gets the parent directory string name of the current directory: (' ... ') Os.pipe () # Creates a pipeline. Returns a pair of file descriptors (R, W) for Read and write Os.popen (command[, mode[, BufSize]) # Open a pipe from a command Os.path.abspath (path)# returns path normalized absolute path Os.path.split (path) # divides path into directory and file name two tuples return Os.path.dirname (PATH) # Returns the directory of path. It is actually the first element of Os.path.split (path) # os.path.basename # returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path) # os.path.exists (path) # If path exists, returns true if path does not exist, return Falseos.path.isabs (PATH) # If path is an absolute path, return Trueos.path.isfile (path) # If path is an existing file, return true. Otherwise returns Falseos.path.isdir (PATH) # returns True if path is a directory that exists. Otherwise return Falseos.path.join (path1[, path2[, ...]) # When multiple paths are combined and returned, the parameters before the first absolute path are ignored Os.path.getatime (path) # Returns the last access time Os.path.getmtime (path) # of the file or directory to which path is pointing The last modified time of the file or directory pointed to by Path Os.name # string indicates the current platform used. Win-> ' NT '; Linux-> ' POSIX ' Os.read (FD, N) # reads up to n bytes from the file descriptor fd, returns a string containing the read bytes, and the file descriptor fd corresponding file has reached the end, returning an empty string. Os.readlink (PATH) # returns the file Os.remove (path) # to which the soft link points to delete the file path. If path is a folder, it will throw oserror; View the following rmdir () to remove a directory. Os.removedirs (Path) # recursively deletes the directory. If the directory is empty, then deleted, and recursively to the previous level of the directory, if also empty, then delete, and so on Os.rename (SRC, DST) # Rename the file or directory, from SRC to dstos.renames (old, new) # Rename the directory recursively, or rename the file accordingly. Os.rmdir (PATH) # Deletes the empty directory specified by path and throws a OSError exception if the directory is not empty. OS.SEP # Operating system specific path delimiter, win under "\ \", Linux under "/" Os.stat (PATH) # Gets the path specified by path information, function is equivalent to C The stat () system call in the API. Os.stat_float_times ([newvalue]) # Determines whether Stat_result displays a timestamp with a float object Os.statvfs (path) # gets file system statistics for the specified path Os.symlink ( SRC, DST) # Create a soft link os.system ("Bash Command") # Run shell command, direct display OS.TCGETPGRP (FD) # Return with Terminal FD (a The associated process group OS.TCSETPGRP (FD, PG) # set up with Terminal FD (an open file descriptor returned by Os.open ()) is linked to PG by the open file descriptor returned by Os.open (). Os.tempnam ([dir[, prefix]]) # Returns a unique path name used to create a temporary file. Os.tmpfile () # Returns a file object with an open mode of (W+B). This file object has no folder entry, no file descriptor, and will be automatically deleted. Os.tmpnam () # Returns a unique path for creating a temporary file Os.ttyname (FD) # Returns a String that represents the terminal device associated with the file descriptor FD. If the FD is not associated with the end device, an exception is thrown. OS.unlink (path) # Delete file path Os.utime (path, times) # Returns the time that the specified path file was accessed and modified. Os.walk (top[, topdown=true[, onerror=none[, Followlinks=false]]) # Output the file name in the folder by walking in the tree, up or down. Os.write (FD, str) # writes the string to the file descriptor FD. Returns the string length actually written
Simple Application
# 列出当前目录下的所有目录[ x for x in os.listdir(‘.‘) if os.path.isdir(x)]# 列出当前目录下所有的.py文件[ x for x in os.listdir(‘.‘) if os.path.isfile(x) and os.path.splitext(x)[1]==‘.py‘]
- Get folder size (directory)
def getdirsize(dir_name): """ 获取目录大小 :param dir_name:目录的绝对路径 :return: 目录的大小 """ size = 0 for root, dirs, files in os.walk(dir_name): for name in files: size += os.path.getsize(os.path.join(root, name)) return size
Python3 module: OS