Python Tutorial (28) - OS file/directory methods

Time: Column:Python views:250

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