os Module in Python
The os module in Python provides a variety of methods for handling files and directories. The table below lists common methods used in the os module:
| No. | Method & Description |
|---|---|
| 1 | os.access(path, mode): Checks the access permissions of a given path. |
| 2 | os.chdir(path): Changes the current working directory. |
| 3 | os.chflags(path, flags): Sets the flags of a given path to numeric flags. |
| 4 | os.chmod(path, mode): Changes the permissions of the specified file or directory. |
| 5 | os.chown(path, uid, gid): Changes the owner of the file. |
| 6 | os.chroot(path): Changes the root directory of the current process. |
| 7 | os.close(fd): Closes the file descriptor fd. |
| 8 | os.closerange(fd_low, fd_high): Closes all file descriptors from fd_low (inclusive) to fd_high (exclusive), ignoring errors. |
| 9 | os.dup(fd): Duplicates file descriptor fd. |
| 10 | os.dup2(fd, fd2): Copies file descriptor fd to fd2. |
| 11 | os.fchdir(fd): Changes the current working directory using a file descriptor. |
| 12 | os.fchmod(fd, mode): Changes the access permissions of a file specified by file descriptor fd. |
| 13 | os.fchown(fd, uid, gid): Changes the ownership of a file specified by file descriptor fd. |
| 14 | os.fdatasync(fd): Forces the file to be written to disk without forcing file metadata updates. |
| 15 | os.fdopen(fd[, mode[, bufsize]]): Opens a file descriptor and returns a file object. |
| 16 | os.fpathconf(fd, name): Returns system configuration information for an open file. |
| 17 | os.fstat(fd): Returns the status of the file descriptor fd, similar to stat(). |
| 18 | os.fstatvfs(fd): Returns filesystem information for the file descriptor fd, similar to statvfs(). |
| 19 | os.fsync(fd): Flushes the file descriptor fd to disk. |
| 20 | os.ftruncate(fd, length): Truncates the file corresponding to file descriptor fd to at most length bytes. |
| 21 | os.getcwd(): Returns the current working directory. |
| 22 | os.getcwdb(): Returns the current working directory as a bytes object. |
| 23 | os.isatty(fd): Returns True if file descriptor fd is open and connected to a tty(-like) device, otherwise False. |
| 24 | os.lchflags(path, flags): Sets the flags of a path, without following symlinks, similar to chflags(). |
| 25 | os.lchmod(path, mode): Changes the permissions of a symlink. |
| 26 | os.lchown(path, uid, gid): Changes the owner of a file without following symlinks, similar to chown(). |
| 27 | os.link(src, dst): Creates a hard link named dst pointing to src. |
| 28 | os.listdir(path): Returns a list of the names of files and directories in the specified path. |
| 29 | os.lseek(fd, pos, how): Sets the position of the file descriptor fd based on how: SEEK_SET (0), SEEK_CUR (1), or SEEK_END (2). |
| 30 | os.lstat(path): Like stat(), but does not follow symbolic links. |
| 31 | os.major(device): Extracts the major device number from a device number (using st_dev or st_rdev from stat()). |
| 32 | os.makedev(major, minor): Combines major and minor device numbers into a device number. |
| 33 | os.makedirs(path[, mode]): Recursively creates directories like mkdir(), including intermediate directories. |
| 34 | os.minor(device): Extracts the minor device number from a device number. |
| 35 | os.mkdir(path[, mode]): Creates a directory named path with numeric mode (default is 0777). |
| 36 | os.mkfifo(path[, mode]): Creates a named pipe with the given mode (default is 0666). |
| 37 | os.mknod(filename[, mode=0600, device]): Creates a filesystem node named filename. |
| 38 | os.open(file, flags[, mode]): Opens a file and sets the required open options; mode is optional. |
| 39 | os.openpty(): Opens a new pseudoterminal pair and returns the file descriptors. |
| 40 | os.pathconf(path, name): Returns system configuration information for a file. |
| 41 | os.pipe(): Creates a pipe and returns a pair of file descriptors (r, w) for reading and writing, respectively. |
| 42 | os.popen(command[, mode[, bufsize]]): Opens a pipe to or from a command. |
| 43 | os.read(fd, n): Reads up to n bytes from file descriptor fd. Returns a string containing the bytes read. If fd reaches EOF, returns an empty string. |
| 44 | os.readlink(path): Returns the file that a symbolic link points to. |
| 45 | os.remove(path): Deletes the file at the specified path. Raises OSError if path is a directory. Use rmdir() to delete directories. |
| 46 | os.removedirs(path): Recursively deletes directories. |
| 47 | os.rename(src, dst): Renames a file or directory from src to dst. |
| 48 | os.renames(old, new): Recursively renames directories and files. |
| 49 | os.rmdir(path): Removes an empty directory. Raises OSError if the directory is not empty. |
| 50 | os.stat(path): Retrieves information about the file or directory at path, similar to the stat() system call in C. |
| 51 | os.stat_float_times([newvalue]): Determines whether timestamps in stat_result are displayed as float objects. |
| 52 | os.statvfs(path): Retrieves filesystem statistics for the specified path. |
| 53 | os.symlink(src, dst): Creates a symbolic link from src to dst. |
| 54 | os.tcgetpgrp(fd): Returns the process group associated with the terminal file descriptor fd. |
| 55 | os.tcsetpgrp(fd, pg): Sets the process group for the terminal file descriptor fd. |
| 56 | os.tempnam([dir[, prefix]]): (Removed in Python 3) Returns a unique path name for creating temporary files. |
| 57 | os.tmpfile(): (Removed in Python 3) Returns an open file object with mode w+b, without a directory entry or file descriptor. It will be automatically deleted. |
| 58 | os.tmpnam(): (Removed in Python 3) Returns a unique path for creating temporary files. |
| 59 | os.ttyname(fd): Returns a string that represents the terminal device associated with file descriptor fd. If fd is not connected to a terminal, raises an exception. |
| 60 | os.unlink(path): Deletes the file at the specified path. |
| 61 | os.utime(path, times): Sets the access and modification times of the file at path. |
| 62 | os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]]): Generates file names in a directory tree by walking either top-down or bottom-up. |
| 63 | os.write(fd, str): Writes the string to file descriptor fd. Returns the number of bytes actually written. |
| 64 | os.path module: Retrieves file attribute information. |
| 65 | os.pardir(): Retrieves the parent directory of the current directory as a string. |
| 66 | os.replace(): Renames a file or directory. |
| 67 | os.startfile(): Opens a file or folder in its associated application (Windows only). |
This table outlines key functions in the os module, which provide comprehensive file and directory management in Python.