Os File Methods
## Python2.x Python OS File/Directory Methods\n\nThe **os** module provides a rich set of methods for handling files and directories. Common methods are listed in the table below:\n\n| No. | Method and Description |\n| --- | --- |\n| 1 | [os.access(path, mode)](#) Test access permissions |\n| 2 | [os.chdir(path)](#) Change the current working directory |\n| 3 | [os.chflags(path, flags)](#) Set the flags for the path to numeric flags. |\n| 4 | [os.chmod(path, mode)](#) Change permissions |\n| 5 | [os.chown(path, uid, gid)](#) Change the file owner |\n| 6 | [os.chroot(path)](#) Change the root directory of the current process |\n| 7 | [os.close(fd)](#) Close file descriptor fd |\n| 8 | [os.closerange(fd_low, fd_high)](#) Close all file descriptors from fd_low (inclusive) to fd_high (exclusive), ignoring errors |\n| 9 | [os.dup(fd)](#) Duplicate file descriptor fd |\n| 10 | [os.dup2(fd, fd2)](#) Duplicate file descriptor fd to fd2 |\n| 11 | [os.fchdir(fd)](#) Change the current working directory via file descriptor |\n| 12 | [os.fchmod(fd, mode)](#) Change the access permissions of a file specified by file descriptor fd. The mode argument is the Unix file access permissions. |\n| 13 | [os.fchown(fd, uid, gid)](#) Change the ownership of a file specified by file descriptor fd. This function modifies the user ID and group ID of the file. |\n| 14 | [os.fdatasync(fd)](#) Force write of file with file descriptor fd to disk, but do not force update of file status information. |\n| 15 | [os.fdopen(fd[, mode[, bufsize]])](#) Return a file object connected to the file descriptor fd. |\n| 16 | [os.fpathconf(fd, name)](#) Return system configuration information for the open file descriptor fd. name specifies the configuration value to retrieve; it may be a string which is the name of a defined system value, these names are specified in a number of standards (POSIX.1, Unix 95, Unix 98, and others). |\n| 17 | [os.fstat(fd)](#) Return status for file descriptor fd, like stat(). |\n| 18 | [os.fstatvfs(fd)](#) Return information about the filesystem containing the file associated with file descriptor fd, like statvfs(). |\n| 19 | [os.fsync(fd)](#) Force write of the file with file descriptor fd to disk. |\n| 20 | [os.ftruncate(fd, length)](#) Truncate the file corresponding to file descriptor fd, so that it is at most length bytes in size. |\n| 21 | [os.getcwd()](#) Return a string representing the current working directory |\n| 22 | [os.getcwdu()](#) Return a Unicode object representing the current working directory |\n| 23 | [os.isatty(fd)](#) Return True if the file descriptor fd is open and connected to a tty(-like) device, else False. |\n| 24 | [os.lchflags(path, flags)](#) Set the flags for the path to numeric flags, like chflags(), but do not follow symbolic links. |\n| 25 | [os.lchmod(path, mode)](#) Change the permissions of a symbolic link. |\n| 26 | [os.lchown(path, uid, gid)](#) Change the owner and group of a symbolic link, similar to chown, but do not follow the link. |\n| 27 | [os.link(src, dst)](#) Create a hard link named dst pointing to src |\n| 28 | [os.listdir(path)](#) Return a list containing the names of the entries in the directory given by path. |\n| 29 | [os.lseek(fd, pos, how)](#) Set the current position of file descriptor fd to position pos, modified by how: SEEK_SET or 0 sets the position relative to the beginning of the file; SEEK_CUR or 1 sets the position relative to the current position; os.SEEK_END or 2 sets the position relative to the end of the file. Valid on Unix and Windows. |\n| 30 | [os.lstat(path)](#) Like stat(), but do not follow symbolic links. |\n| 31 | [os.major(device)](#) Extract the device major number from a raw device number (use the st_dev or st_rdev field from stat). |\n| 32 | [os.makedev(major, minor)](#) Compose a raw device number from the major and minor device numbers. |\n| 33 | [os.makedirs(path[, mode])](#) Recursive directory creation function. Like mkdir(), but makes all intermediate-level directories needed to contain the leaf directory. |\n| 34 | [os.minor(device)](#) Extract the device minor number from a raw device number (use the st_dev or st_rdev field from stat). |\n| 35 | [os.mkdir(path[, mode])](#) Create a directory named path with numeric mode mode. The default mode is 0777 (octal). |\n| 36 | [os.mkfifo(path[, mode])](#) Create a named pipe (FIFO) with numeric mode mode. The default mode is 0666 (octal). |\n| 37 | [os.mknod(filename[, mode=0600, device])](#) Create a filesystem node (file, device special file or named pipe) named filename. |\n| 38 | [os.open(file, flags[, mode])](#) Open a file, setting the required open flags and mode argument (optional). |\n| 39 | [os.openpty()](#) Open a new pseudo-terminal pair. Return a pair of file descriptors (r, w) for reading and writing. |\n| 40 | [os.pathconf(path, name)](#) Return system configuration information for the file named path. |\n| 41 | [os.pipe()](#) Create a pipe. Return a pair of file descriptors (r, w) for reading and writing. |\n| 42 | [os.popen(command[, mode[, bufsize]])](#) Open a pipe to command. |\n| 43 | [os.read(fd, n)](#) Read at most n bytes from file descriptor fd. Return a bytes object containing the bytes read. If the end of the file referred to by fd has been reached, an empty bytes object is returned. |\n| 44 | [os.readlink(path)](#) Return a string representing the path to which the symbolic link points. |\n| 45 | [os.remove(path)](#) Remove (delete) the file path. If path is a directory, OSError is raised; see rmdir() below for removing a directory. |\n| 46 | [os.removedirs(path)](#) Recursive directory removal function. |\n| 47 | [os.rename(src, dst)](#) Rename the file or directory src to dst. |\n| 48 | [os.renames(old, new)](#) Recursive renaming or directory renaming function. |\n| 49 | [os.rmdir(path)](#) Remove (delete) the directory path. Only works when the directory is empty; otherwise, OSError is raised. |\n| 50 | [os.stat(path)](#) Get the status of the file or directory path. Equivalent to the stat() system call. |\n| 51 | [os.stat_float_times()](#) Determine whether stat_result represents time stamps as float objects. |\n| 52 | [os.statvfs(path)](#) Get the filesystem statistics for the given path. |\n| 53 | [os.symlink(src, dst)](#) Create a symbolic link pointing to src named dst. |\n| 54 | [os.tcgetpgrp(fd)](#) Return the process group associated with the terminal file descriptor fd (which must be opened by os.open()). |\n| 55 | [os.tcsetpgrp(fd, pg)](#) Set the process group associated with the terminal file descriptor fd (which must be opened by os.open()) to pg. |\n| 56 | [os.tempnam([dir[, prefix]])](#) Return a unique pathname for creating a temporary file. |\n| 57 | [os.tmpfile()](#) Return a new file object opened in mode 'w+b'. This file object has no directory entry, no file descriptor, and will be deleted automatically. |\n| 58 | [os.tmpnam()](#) Return a unique pathname for creating a temporary file. |\n| 59 | [os.ttyname(fd)](#) Return a string which specifies the terminal device associated with file descriptor fd. If fd is not associated with a terminal device, an exception is raised. |\n| 60 | [os.unlink(path)](#) Remove (delete) the file path. |\n| 61 | [os.utime(path, times)](#) Set the access and modified times of the file specified by path. |\n| 62 | [os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])](#) Generate the file names in a directory tree by walking the tree either top-down or bottom-up. |\n| 63 | [os.write(fd, str)](#) Write the string str to file descriptor fd. Return the number of bytes actually written. |\n| 64 | [os.path Module](#) Get file attribute information. |\n\n### Reference Links:\n\n* https://blog.konghy.cn/2015/08/02/python-os/\n* https://python.usyiyi.cn/python_278/library/os.html
YouTip