Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python OS file/directory methods

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

os.access(path, mode)

Check the permission mode
2

os.chdir(path)

Change the current working directory
3

os.chflags(path, flags)

Set the flags of a path to numeric flags.
4

os.chmod(path, mode)

Change permissions
5

os.chown(path, uid, gid)

Change the file owner
6

os.chroot(path)

Change the root directory of the current process
7

os.close(fd)

Close the 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 the file descriptor fd
10

os.dup2(fd, fd2)

Duplicate one file descriptor fd onto another fd2
11

os.fchdir(fd)

Change the current working directory through 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)

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

os.fdatasync(fd)

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

os.fdopen(fd[, mode[, bufsize]])

Create a file object from a file descriptor fd and return that file object
16

os.fpathconf(fd, name)

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

os.fstat(fd)

Return the status of the file descriptor fd, like stat().
18

os.fstatvfs(fd)

Returns information about the file system containing the file with file descriptor fd, similar to statvfs()
19

os.fsync(fd)

Force the file with file descriptor fd to be written to disk.
20

os.ftruncate(fd, length)

Truncate the file corresponding to the 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)

Return True if the file descriptor fd is open and connected to a tty(-like) device, otherwise False.
24

os.lchflags(path, flags)

Set the flags of a path to numeric flags, similar to chflags(), but without following symbolic links
25

os.lchmod(path, mode)

Change the permissions of a linked file
26

os.lchown(path, uid, gid)

Change the file owner, similar to chown, but without following links.
27

os.link(src, dst)

Create a hard link named by the parameter dst pointing to the parameter src
28

os.listdir(path)

Return a list of the names of files or folders contained in the directory specified by path.
29

os.lseek(fd, pos, how)

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

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 the major and minor device numbers
33

os.makedirs(path[, mode])

Recursive directory creation function. Like mkdir(), but all intermediate-level directories created need to contain subdirectories.
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 directory named path with the numeric mode mode. The default mode is 0777 (octal).
36

os.mkfifo(path[, mode])

Create a named pipe with a numeric mode, defaulting to 0666 (octal)
37

os.mknod(filename[, mode=0600, device])


Create a file system node named filename (a file, device special file or named pipe).

| | 38 |

os.open(file, flags[, mode])

Open a file and set the required opening options; the mode parameter is optional | | 39 |

os.openpty()

Open a new pseudo-terminal pair. Returns the file descriptors of the pty and tty. | | 40 |

os.pathconf(path, name)

Return system configuration information for the related file. | | 41 |

os.pipe()

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 |

os.read(fd, n)

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 |

os.readlink(path)

Return the file pointed to by a symbolic link | | 45 |

os.remove(path)

Delete the file at path. If path is a directory, OSError is raised; see rmdir() below for deleting a directory. | | 46 |

os.removedirs(path)

Recursively delete directories. | | 47 |

os.rename(src, dst)

Rename a file or directory from src to dst | | 48 |

os.renames(old, new)

Recursively rename directories, and can also rename files. | | 49 |

os.rmdir(path)

Delete the empty directory specified by path; if the directory is not empty, raise an OSError exception. | | 50 |

os.stat(path)

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

| | 52 |

os.statvfs(path)

Get file system statistics for the specified path | | 53 |

os.symlink(src, dst)

Create a symbolic link | | 54 |

os.tcgetpgrp(fd)

Return the process group associated with the terminal fd (an open file descriptor returned by os.open()) | | 55 |

os.tcsetpgrp(fd, pg)

Set the process group associated with the terminal fd (an open file descriptor returned by os.open()) to pg. | | 56 |

os.tempnam([dir[, prefix]])

Returns a unique pathname for creating a temporary file. | | 57 |

os.tmpfile()

Returns a file object opened in mode (w+b). This file object has no folder entry, no file descriptor, and will be deleted automatically. | | 58 |

os.tmpnam()

Returns a unique path for creating a temporary file | | 59 |

os.ttyname(fd)

Return a string representing the terminal device associated with the file descriptor fd. If fd is not associated with a terminal device, raise an exception. | | 60 |

os.unlink(path)

Delete a file | | 61 |

os.utime(path, times)

Return the access and modification times of the file specified by path. | | 62 |

os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])

Output the file names in a directory by walking the tree upwards or downwards. | | 63 |

os.write(fd, str)

Write a string to the file descriptor fd. Return the actual number of characters written | | 64 |

The os.path module

Gets the attribute information of a file. |

Reference: