Python OS File/Directory Methods

osThe module provides a very rich set of methods for handling files and directories. Common methods are shown in the following table:

No.Method and Description
1

os.access(path, mode)


Check permission mode
2

os.chdir(path)


Change current working directory
3

os.chflags(path, flags)


Set the flags of path to numeric flags.
4

os.chmod(path, mode)


Change permissions
5

os.chown(path, uid, gid)


Change file owner
6

os.chroot(path)


Change root directory of the current process
7

os.close(fd)


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

os.dup2(fd, fd2)


Duplicate file descriptor fd to another fd2
11

os.fchdir(fd)


Change current working directory via file descriptor
12

os.fchmod(fd, mode)


Change the access permissions of a file, the file is specified by parameter fd, parameter mode is the file access permission under Unix.
13

os.fchown(fd, uid, gid)


Change the ownership of a file, this function changes the user ID and user group ID of a file, the file is specified by file descriptor fd.
14

os.fdatasync(fd)


Force write the file to disk, the file is specified by file descriptor fd, but does not force update of file status information.
15

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


Create a file object via file descriptor fd and return this file object
16

os.fpathconf(fd, name)


Return system configuration information of an open file. name is the value of the system configuration to retrieve, it may be a string defining a system value, these names are specified in many standards (POSIX.1, Unix 95, Unix 98, and others).
17

os.fstat(fd)


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

os.fstatvfs(fd)


Return the file system information of the file containing file descriptor fd, like 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 file descriptor fd, so 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)


If file descriptor fd is open and connected to a tty(-like) device, return true, otherwise False.
24

os.lchflags(path, flags)


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

os.lchmod(path, mode)


Change the permissions of a link file
26

os.lchown(path, uid, gid)


Change file owner, similar to chown, but does not track links.
27

os.link(src, dst)


Create a hard link named parameter dst, pointing to parameter src
28

os.listdir(path)


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

os.lseek(fd, pos, how)


Set the current position of file descriptor fd to pos, modified by how: SEEK_SET or 0 sets pos calculated from the beginning 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 soft links
31

os.major(device)


Extract device major number from raw device number (use st_dev or st_rdev field in stat).
32

os.makedev(major, minor)


Compose a raw device number from major and minor device numbers
33

os.makedirs(path[, mode])


Recursive folder creation function. Like mkdir(), but creates all intermediate-level folders required to contain subfolders.
34

os.minor(device)


Extract device minor number from raw device number (use st_dev or st_rdev field in stat).
35

os.mkdir(path[, mode])


Create a folder named path with mode in numeric mode. Default mode is 0777 (octal).
36

os.mkfifo(path[, mode])


Create a named pipe, mode is numeric, default is 0666 (octal)
37

os.mknod(filename[, mode=0600, device])
Create a file system node named filename (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. Return the pty and tty file descriptors.
40

os.pathconf(path, name)


Return the system configuration information of the relevant file.
41

os.pipe()


Create a pipe. Return 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 file descriptor fd, return a string containing the read bytes. If the file corresponding to file descriptor fd has reached the end, return an empty string.
44

os.readlink(path)


Return the file pointed to by the soft link
45

os.remove(path)


Delete the file whose path is path. If path is a folder, OSError will be raised; see rmdir() below to delete a directory.
46

os.removedirs(path)


Recursively delete directories.
47

os.rename(src, dst)


Rename file or directory, from src to dst
48

os.renames(old, new)


Recursively rename directories, can also rename files.
49

os.rmdir(path)


Delete the empty directory specified by path. If the directory is not empty, an OSError exception is raised.
50

os.stat(path)


Get information of the path specified by path, function 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 soft link
54

os.tcgetpgrp(fd)


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

os.tcsetpgrp(fd, pg)


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

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


Return a unique path name for creating a temporary file.
57

os.tmpfile()


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

os.tmpnam()


Return a unique path for creating a temporary file
59

os.ttyname(fd)


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

os.unlink(path)


Delete file
61

os.utime(path, times)


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

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


Output file names in folders by walking the tree, top-down or bottom-up.
63

os.write(fd, str)


Write a string to file descriptor fd. Return the actual written string length.
64

os.path module


Get file attribute information.

Reference address:

  • https://blog.konghy.cn/2015/08/02/python-os/
  • https://python.usyiyi.cn/python_278/library/os.html
  • Other extensions