Python3 OS File/Directory Methods
The os module provides a plethora of methods to handle files and directories. Commonly used methods are listed in the table below:
No. | Method and Description |
---|---|
1 | os.access(path, mode) <br>Check access permissions of a path |
2 | os.chdir(path) <br>Change the current working directory |
3 | os.chflags(path, flags) <br>Set the flags of the path to numeric flags |
4 | os.chmod(path, mode) <br>Change permissions |
5 | os.chown(path, uid, gid) <br>Change file owner |
6 | os.chroot(path) <br>Change the root directory of the current process |
7 | os.close(fd) <br>Close file descriptor fd |
8 | os.closerange(fd_low, fd_high) <br>Close all file descriptors from fd_low (inclusive) to fd_high (exclusive), ignoring errors |
9 | os.dup(fd) <br>Duplicate file descriptor fd |
10 | os.dup2(fd, fd2) <br>Duplicate file descriptor fd to fd2 |
11 | os.fchdir(fd) <br>Change the current working directory via file descriptor |
12 | os.fchmod(fd, mode) <br>Change the access permissions of a file specified by fd to the mode specified |
13 | os.fchown(fd, uid, gid) <br>Change the ownership of a file specified by fd to the user ID and group ID |
14 | os.fdatasync(fd) <br>Force write file to disk specified by file descriptor fd, without forcing update of metadata |
15 | os.fdopen(fd[, mode[, bufsize]]) <br>Create a file object from the file descriptor fd |
16 | os.fpathconf(fd, name) <br>Return system configuration information for an open file, where name is the configuration value to retrieve |
17 | os.fstat(fd) <br>Return the status of the file descriptor fd, similar to stat() |
18 | os.fstatvfs(fd) <br>Return information about the filesystem containing the file associated with file descriptor fd, equivalent to statvfs() in Python 3.3 |
19 | os.fsync(fd) <br>Force write of file with file descriptor fd to disk |
20 | os.ftruncate(fd, length) <br>Truncate the file corresponding to file descriptor fd, so that it is at most length bytes in size |
21 | os.getcwd() <br>Return the current working directory |
22 | os.getcwdb() <br>Return a byte string representing the current working directory |
23 | os.isatty(fd) <br>Return True if the file descriptor fd is open and connected to a tty(-like) device, else False |
24 | os.lchflags(path, flags) <br>Set the flags of path to numeric flags, similar to chflags(), but does not follow symbolic links |
25 | os.lchmod(path, mode) <br>Change the permissions of a symbolic link |
26 | os.lchown(path, uid, gid) <br>Change the owner of a file, similar to chown, but does not follow symbolic links |
27 | os.link(src, dst) <br>Create a hard link pointing to src named dst |
28 | os.listdir(path) <br>Return a list of entries in the directory given by path |
29 | os.lseek(fd, pos, how) <br>Set the current position of file descriptor fd to position pos, modified by how: SEEK_SET or 0 to set position relative to the beginning of the file; SEEK_CUR or 1 to set it relative to the current position; SEEK_END or 2 to set it relative to the end of the file. Valid in Unix and Windows |
30 | os.lstat(path) <br>Similar to stat(), but does not follow symbolic links |
31 | os.major(device) <br>Extract the device major number from the raw device number (using stat.st_dev or stat.st_rdev) |
32 | os.makedev(major, minor) <br> Composes a raw device number from the major and minor device numbers. |
33 | os.makedirs(path[, mode]) <br> Recursive directory creation function. Like mkdir(), but all intermediate-level directories must also contain subdirectories. |
34 | os.minor(device) <br> Extracts the device minor number from the raw device number (using the st_dev or st_rdev field from stat). |
35 | os.mkdir(path[, mode]) <br> Creates a directory named path with numeric mode mode. The default mode is 0777 (octal). |
36 | os.mkfifo(path[, mode]) <br> Creates a named pipe with mode as a numeric mode, defaulting to 0666 (octal). |
37 | os.mknod(filename[, mode=0600, device]) <br> Creates a filesystem node (file, device special file, or named pipe) named filename. |
38 | os.open(file, flags[, mode]) <br> Opens a file and sets the required opening options; the mode parameter is optional. |
39 | os.openpty() <br> Opens a new pseudo-terminal pair. Returns file descriptors for the pty and tty. |
40 | os.pathconf(path, name) <br> Returns system configuration information relevant to a file. |
41 | os.pipe() <br> Creates a pipe. Returns a pair of file descriptors (r, w) for reading and writing. |
42 | os.popen(command[, mode[, bufsize]]) <br> Opens a pipe to or from command. |
43 | os.read(fd, n) <br> Reads up to n bytes from file descriptor fd, returning a string containing the bytes read. Returns an empty string if the end of the file referred to by fd is reached. |
44 | os.readlink(path) <br> Returns the path to which the symbolic link points. |
45 | os.remove(path) <br> Removes the file path. If path is a directory, an OSError is raised; see rmdir() below to remove a directory. |
46 | os.removedirs(path) <br> Recursively removes directories. |
47 | os.rename(src, dst) <br> Renames the file or directory from src to dst. |
48 | os.renames(old, new) <br> Recursively renames directories and files. |
49 | os.rmdir(path) <br> Removes the directory path, which must be empty. Raises an OSError if the directory is not empty. |
50 | os.stat(path) <br> Gets information about the path specified, equivalent to the stat() system call in C. |
51 | os.stat_float_times([newvalue]) <br> Determines whether stat_result displays timestamps as float objects. |
52 | os.statvfs(path) <br> Gets statistics about the filesystem at the specified path. |
53 | os.symlink(src, dst) <br> Creates a symbolic link. |
54 | os.tcgetpgrp(fd) <br> Returns the process group associated with the terminal connected to fd (an open file descriptor returned by os.open()). |
55 | os.tcsetpgrp(fd, pg) <br> Sets the process group associated with the terminal connected to fd (an open file descriptor returned by os.open()) to pg. |
56 | os.tempnam([dir[, prefix]]) <br> Removed in Python 3. Returns a unique path name for creating a temporary file. |
57 | os.tmpfile() <br> Removed in Python 3. Returns a file object opened in mode w+b. This file object has no directory entries and no file descriptor and will be automatically deleted. |
58 | os.tmpnam() <br> Removed in Python 3. Returns a unique path for creating a temporary file. |
59 | os.ttyname(fd) <br> Returns a string that identifies the terminal device associated with file descriptor fd. Raises an exception if fd is not associated with a terminal device. |
60 | os.unlink(path) <br> Deletes the file path. |
61 | os.utime(path, times) <br> Sets the access and modified times of the file specified by path. |
62 | os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]]) <br>Outputs filenames within a directory by traversing the tree either top-down or bottom-up. |
63 | os.write(fd, str) <br>Writes a string to the file descriptor fd. Returns the number of characters actually written. |
64 | os.path module <br>Retrieves attribute information of a file. |
65 | os.pardir() <br>Gets the parent directory of the current directory, displaying the directory name as a string. |
66 | os.replace() <br>Renames a file or directory. |