Python OS File/Directory Methods
osThe module provides a very rich set of methods for handling files and directories. Common methods are shown in the following table:
| No. | Method and Description |
|---|---|
| 1 | Check permission mode |
| 2 | Change current working directory |
| 3 | Set the flags of path to numeric flags. |
| 4 | Change permissions |
| 5 | Change file owner |
| 6 | Change root directory of the current process |
| 7 | Close file descriptor fd |
| 8 | os.closerange(fd_low, fd_high) Close all file descriptors from fd_low (inclusive) to fd_high (exclusive), errors are ignored |
| 9 | Duplicate file descriptor fd |
| 10 | Duplicate file descriptor fd to another fd2 |
| 11 | Change current working directory via file descriptor |
| 12 | Change the access permissions of a file, the file is specified by parameter fd, parameter mode is the file access permission under Unix. |
| 13 | Change the ownership of a file, this function changes the user ID and user group ID of a file, the file is specified by file descriptor fd. |
| 14 | Force write the file to disk, the file is specified by file descriptor fd, but does not force update of file status information. |
| 15 | os.fdopen(fd[, mode[, bufsize]]) Create a file object via file descriptor fd and return this file object |
| 16 | Return system configuration information of an open file. name is the value of the system configuration to retrieve, it may be a string defining a system value, these names are specified in many standards (POSIX.1, Unix 95, Unix 98, and others). |
| 17 | Return the status of file descriptor fd, like stat(). |
| 18 | Return the file system information of the file containing file descriptor fd, like statvfs() |
| 19 | Force the file with file descriptor fd to be written to disk. |
| 20 | Truncate the file corresponding to file descriptor fd, so it cannot exceed the file size. |
| 21 | Return the current working directory |
| 22 | Return a Unicode object of the current working directory |
| 23 | If file descriptor fd is open and connected to a tty(-like) device, return true, otherwise False. |
| 24 | Set the flags of path to numeric flags, similar to chflags(), but without soft links |
| 25 | Change the permissions of a link file |
| 26 | Change file owner, similar to chown, but does not track links. |
| 27 | Create a hard link named parameter dst, pointing to parameter src |
| 28 | Return a list of names of files or folders contained in the folder specified by path |
| 29 | Set the current position of file descriptor fd to pos, modified by how: SEEK_SET or 0 sets pos calculated from the beginning of the file; SEEK_CUR or 1 calculates from the current position; os.SEEK_END or 2 starts from the end of the file. Valid on Unix and Windows. |
| 30 | Like stat(), but without soft links |
| 31 | Extract device major number from raw device number (use st_dev or st_rdev field in stat). |
| 32 | Compose a raw device number from major and minor device numbers |
| 33 | Recursive folder creation function. Like mkdir(), but creates all intermediate-level folders required to contain subfolders. |
| 34 | Extract device minor number from raw device number (use st_dev or st_rdev field in stat). |
| 35 | Create a folder named path with mode in numeric mode. Default mode is 0777 (octal). |
| 36 | Create a named pipe, mode is numeric, default is 0666 (octal) |
| 37 | os.mknod(filename[, mode=0600, device]) |
| 38 | Open a file and set the required opening options, the mode parameter is optional |
| 39 | Open a new pseudo-terminal pair. Return the pty and tty file descriptors. |
| 40 | Return the system configuration information of the relevant file. |
| 41 | Create a pipe. Return a pair of file descriptors (r, w) for reading and writing respectively |
| 42 | os.popen(command[, mode[, bufsize]]) Open a pipe from a command |
| 43 | Read at most n bytes from file descriptor fd, return a string containing the read bytes. If the file corresponding to file descriptor fd has reached the end, return an empty string. |
| 44 | Return the file pointed to by the soft link |
| 45 | Delete the file whose path is path. If path is a folder, OSError will be raised; see rmdir() below to delete a directory. |
| 46 | Recursively delete directories. |
| 47 | Rename file or directory, from src to dst |
| 48 | Recursively rename directories, can also rename files. |
| 49 | Delete the empty directory specified by path. If the directory is not empty, an OSError exception is raised. |
| 50 | Get information of the path specified by path, function equivalent to the stat() system call in the C API. |
| 51 | os.stat_float_times([newvalue]) |
| 52 | Get file system statistics for the specified path |
| 53 | Create a soft link |
| 54 | Return the process group associated with terminal fd (an open file descriptor returned by os.open()) |
| 55 | Set the process group associated with terminal fd (an open file descriptor returned by os.open()) to pg. |
| 56 | Return a unique path name for creating a temporary file. |
| 57 | Return a file object opened in mode (w+b). This file object has no folder entry, no file descriptor, and will be automatically deleted. |
| 58 | Return a unique path for creating a temporary file |
| 59 | Return a string representing the terminal device associated with file descriptor fd. If fd is not associated with a terminal device, an exception is raised. |
| 60 | Delete file |
| 61 | Return the access and modification times of the specified path file. |
| 62 | os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]]) Output file names in folders by walking the tree, top-down or bottom-up. |
| 63 | Write a string to file descriptor fd. Return the actual written string length. |
| 64 | Get file attribute information. |
Reference address:
- https://blog.konghy.cn/2015/08/02/python-os/
- https://python.usyiyi.cn/python_278/library/os.html Other extensions