Python OS file/directory methods
os module provides a very rich set of methods for handling files and directories. The commonly used methods are shown in the following table:
| No. | Method and description |
|---|---|
| 1 | |
| Check the permission mode | |
| 2 | |
| Change the current working directory | |
| 3 | |
| Set the flags of a path to numeric flags. | |
| 4 | |
| Change permissions | |
| 5 | |
| Change the file owner | |
| 6 | |
| Change the root directory of the current process | |
| 7 | |
| Close the file descriptor fd | |
| 8 | |
| Close all file descriptors from fd_low (inclusive) to fd_high (exclusive); errors are ignored | |
| 9 | |
| Duplicate the file descriptor fd | |
| 10 | |
| Duplicate one file descriptor fd onto another fd2 | |
| 11 | |
| Change the current working directory through a file descriptor | |
| 12 | |
| Change the access permissions of a file specified by the parameter fd; the parameter mode is the Unix file access permission. | |
| 13 | |
| Change the ownership of a file; this function changes the user ID and group ID of the file specified by the file descriptor fd. | |
| 14 | |
| Force a file specified by the file descriptor fd to be written to disk, but do not force the file's status information to be updated. | |
| 15 | |
| Create a file object from a file descriptor fd and return that file object | |
| 16 | |
| Return system configuration information for an open file. name is the system configuration value to retrieve; it may be a string defining a system value, and these names are specified in many standards (POSIX.1, Unix 95, Unix 98 and others). | |
| 17 | |
| Return the status of the file descriptor fd, like stat(). | |
| 18 | |
| Returns information about the file system containing the file with file descriptor fd, similar to statvfs() | |
| 19 | |
| Force the file with file descriptor fd to be written to disk. | |
| 20 | |
| Truncate the file corresponding to the file descriptor fd so that it cannot exceed the file size. | |
| 21 | |
| Return the current working directory | |
| 22 | |
| Return a Unicode object of the current working directory | |
| 23 | |
| Return True if the file descriptor fd is open and connected to a tty(-like) device, otherwise False. | |
| 24 | |
| Set the flags of a path to numeric flags, similar to chflags(), but without following symbolic links | |
| 25 | |
| Change the permissions of a linked file | |
| 26 | |
| Change the file owner, similar to chown, but without following links. | |
| 27 | |
| Create a hard link named by the parameter dst pointing to the parameter src | |
| 28 | |
| Return a list of the names of files or folders contained in the directory specified by path. | |
| 29 | |
| Set the current position of the file descriptor fd to pos, with how specifying the method: SEEK_SET or 0 sets pos relative to the start 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 following symbolic links | |
| 31 | |
| Extract the device major number from a raw device number (using the st_dev or st_rdev field in stat). | |
| 32 | |
| Compose a raw device number from the major and minor device numbers | |
| 33 | |
| Recursive directory creation function. Like mkdir(), but all intermediate-level directories created need to contain subdirectories. | |
| 34 | |
| Extract the device minor number from a raw device number (using the st_dev or st_rdev field in stat). | |
| 35 | |
| Create a directory named path with the numeric mode mode. The default mode is 0777 (octal). | |
| 36 | |
| Create a named pipe with a numeric mode, defaulting to 0666 (octal) | |
| 37 |
Create a file system node named filename (a file, device special file or named pipe). | | 38 | Open a file and set the required opening options; the mode parameter is optional | | 39 | Open a new pseudo-terminal pair. Returns the file descriptors of the pty and tty. | | 40 | Return system configuration information for the related file. | | 41 | Create a pipe. Returns 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 the file descriptor fd and return a string containing the bytes read; if the file corresponding to fd has reached its end, return an empty string. | | 44 | Return the file pointed to by a symbolic link | | 45 | Delete the file at path. If path is a directory, OSError is raised; see rmdir() below for deleting a directory. | | 46 | Recursively delete directories. | | 47 | Rename a file or directory from src to dst | | 48 | Recursively rename directories, and can also rename files. | | 49 | Delete the empty directory specified by path; if the directory is not empty, raise an OSError exception. | | 50 | Get information about the path specified by path, equivalent to the stat() system call in the C API. | | 51 |os.stat_float_times([newvalue])
Determine whether stat_result displays timestamps as float objects
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
Output the file names in a directory by walking the tree upwards or downwards. | | 63 | Write a string to the file descriptor fd. Return the actual number of characters written | | 64 | Gets the attribute information of a file. |Reference:
- https://blog.konghy.cn/2015/08/02/python-os/
- https://python.usyiyi.cn/python_278/library/os.html