Python2.x Python OS File/Directory Methods
The os module provides a wealth 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), ignoring errors |
| 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, specified by the parameter fd. 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 specified by the file descriptor fd. |
| 14 | os.fdatasync(fd) Force writing the file to disk, specified by the file descriptor fd, but does not force the update 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, 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 filesystem 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 that 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, return False. |
| 24 | os.lchflags(path, flags) Set the flags of the path to numeric flags, similar to chflags(), but without following symbolic links |
| 25 | os.lchmod(path, mode) Modify the permissions of a linked file |
| 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 the 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; how modifies the way: 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 calculates from the end of the file. Valid in Unix and Windows |
| 30 | os.lstat(path) Like stat(), but without following symbolic links |
| 31 | os.major(device) Extract the device major number from a raw device number (using the st_dev or st_rdev field in stat). |
| 32 | os.makedev(major, minor) Compose a raw device number from 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 a raw device number (using the st_dev or st_rdev field in stat). |
| 35 | os.mkdir(path[, mode]) Create a folder named path with numeric mode. The default mode is 0777 (octal). |
YouTip