Python2.x Python OS File/Directory Methods
The os module provides a very rich set of methods for handling files and directories. The commonly used methods are listed in the table below:
| No. | Method & Description |
|---|---|
| 1 | os.access(path, mode) Test access permissions |
| 2 | os.chdir(path) Change the current working directory |
| 3 | os.chflags(path, flags) Set the flags of the path to numeric flags. |
| 4 | os.chmod(path, mode) Change permissions |
| 5 | os.chown(path, uid, gid) Change file owner |
| 6 | os.chroot(path) Change the root directory of the current process |
| 7 | os.close(fd) 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 | os.dup(fd) Duplicate file descriptor fd |
| 10 | os.dup2(fd, fd2) Duplicate one file descriptor fd to another fd2 |
| 11 | os.fchdir(fd) Change the current working directory via a file descriptor |
| 12 | os.fchmod(fd, mode) Change the access permissions of a file, the file is specified by the parameter fd, and the parameter mode is the Unix file access permission. |
| 13 | os.fchown(fd, uid, gid) Modify the ownership of a file, this function modifies the user ID and group ID of a file, which is specified by the file descriptor fd. |
| 14 | os.fdatasync(fd) Force writing the file to disk, the file is specified by the file descriptor fd, but does not force updating of the file's status information. |
| 15 | os.fdopen(fd[, mode[, bufsize]]) Create a file object through file descriptor fd and return this file object |
| 16 | os.fpathconf(fd, name) Return system configuration information for 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 | os.fstat(fd) Return the status of file descriptor fd, like stat(). |
| 18 | os.fstatvfs(fd) Return information about the file system containing the file associated with file descriptor fd, like statvfs() |
| 19 | os.fsync(fd) Force writing the file with file descriptor fd to the hard disk. |
| 20 | os.ftruncate(fd, length) Truncate the file corresponding to file descriptor fd, so it cannot exceed the file size. |
| 21 | os.getcwd() Return the current working directory |
| 22 | os.getcwdu() Return a Unicode object of the current working directory |
| 23 | os.isatty(fd) If file descriptor fd is open and connected to a tty(-like) device, return true, otherwise False. |
| 24 | os.lchflags(path, flags) Set the flags of the path to numeric flags, similar to chflags(), but without soft links |
| 25 | os.lchmod(path, mode) Modify link file permissions |
| 26 | os.lchown(path, uid, gid) Change file owner, similar to chown, but does not follow links. |
| 27 | os.link(src, dst) Create a hard link, named parameter dst, pointing to parameter src |
| 28 | os.listdir(path) Return a list of names of files or folders contained in the folder specified by path. |
| 29 | os.lseek(fd, pos, how) 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 in Unix, Windows |
| 30 | os.lstat(path) Like stat(), but without soft links |
| 31 | os.major(device) Extract the device major number from the raw device number (using the st_dev or st_rdev field in stat). |
| 32 | os.makedev(major, minor) Compose a raw device number from the major and minor device numbers |
| 33 | os.makedirs(path[, mode]) Recursive folder creation function. Like mkdir(), but creates all intermediate-level folders needed to contain the subfolder. |
| 34 | os.minor(device) Extract the device minor number from the raw device number (using the st_dev or st_rdev field in stat). |
| 35 | os.mkdir(path[, mode]) Create a folder named path with a numeric mode. The default mode is 0777 ( |
YouTip