Python3 OS file/directory method

Python 3 OS File/Directory Methods

os provides a rich set of methods for working with files and directories. Commonly used methods are shown in the following table:

Number Method and Description
1

os.access(path, mode)

Verify the permission mode

2

os.chdir(path)

Change the current working directory

3

os.chflags(path, flags)

Set the flags of the 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 the current process’s root directory

7

os.close(fd)

Close the 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 one file descriptor fd to another fd2

11

os.fchdir(fd)

Change the current working directory using a file descriptor.

12

os.fchmod(fd, mode)

Changes the access permissions of a file specified by the parameter fd. The parameter mode is the file access permissions under Unix.

13

os.fchown(fd, uid, gid)

Modifies the ownership of a file. This function modifies the user ID and group ID of the file specified by the file descriptor fd.

14

os.fdatasync(fd)

Forces writing the file identified by file descriptor fd to disk, but does not force an update of the file’s status information.

15

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

Creates a file object from file descriptor fd and returns it.

16

os.fpathconf(fd, name)

Returns system configuration information for an open file. name is the system configuration value to retrieve. It may be a string defining a system value, as specified in various 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)

Returns information about the file system containing the file descriptor fd. Python 3.3. Equivalent to statvfs().

19

os.fsync(fd)

Forces the file descriptor fd to be written to disk.

20

os.ftruncate(fd, length)

Truncates the file corresponding to file descriptor fd, so that it cannot exceed the maximum file size.

21

os.getcwd()

Returns the current working directory.

22

os.getcwdu()

Returns a Unicode object representing the current working directory.

23

os.isatty(fd)

Returns true if file descriptor fd is open and connected to a tty(-like) device, false otherwise.

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)

Modify the permissions of the linked file.

26

os.lchown(path, uid, gid)

Change the file owner, similar to chown, but without following links.

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 the files or directories contained in the directory specified by path.

29

os.lseek(fd, pos, how)

Set the current position of file descriptor fd to pos, and modify it using how: SEEK_SET or 0 sets pos from the beginning of the file; SEEK_CUR or 1 sets pos from the current position; os.SEEK_END or 2 sets pos 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 the device major number from the raw device number (Use the st_dev or st_rdev field in stat.)

32

os.makedev(major, minor)

Compose a raw device number from the major and minor device numbers.

33

os.makedirs(path[, mode])

Recursive directory creation function. Like mkdir(), but all intermediate-level directories created must include subdirectories.

34

os.minor(device)

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

35

os.mkdir(path[, mode])

Create a directory named path with a numeric mode. The default mode is 0777 (octal).

36

os.mkfifo(path[, mode])

Create a named pipe with a numeric mode. The default mode is 0666 (octal).

37

os.mknod(filename[, mode=0600, device])
Create a file system node (file, device special file, or named pipe) named filename.

38

os.open(file, flags[, mode])

Opens a file with the required open options. The mode parameter is optional.

39

os.openpty()

Opens a new pseudo-terminal pair. Returns the file descriptors for the pty and tty.

40

os.pathconf(path, name)

Returns system configuration information for the associated file.

41

os.pipe()

Creates a pipe. Returns a pair of file descriptors (r, w) for reading and writing, respectively.

42

os.popen(command[, mode[, bufsize]])

Opens a pipe from a command

43

os.read(fd, n)

Reads up to n bytes from file descriptor fd and returns a string containing the bytes read. If the end of the file corresponding to file descriptor fd has been reached, an empty string is returned.

44

os.readlink(path)

Returns the file pointed to by the soft link.

45

os.remove(path)

Deletes the file at path. If path is a directory, an OSError will be raised; see rmdir() below for deleting a directory.

46

os.removedirs(path)

Recursively delete directories.

47

os.rename(src, dst)

Renames a file or directory from src to dst.

48

os.renames(old, new)

Recursively renames directories and may also rename files.

49

os.rmdir(path)

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

50

os.stat(path)

Get information about the path specified by path. This function is equivalent to the stat() system call in the C API.

51

os.stat_float_times([newvalue])
Determines 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)

Creates a soft link

54

os.tcgetpgrp(fd)

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

55

os.tcsetpgrp(fd, pg)

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

56

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

Removed in Python 3. Returns a unique pathname for creating a temporary file.

57

os.tmpfile()

Removed in Python 3. Returns a file object opened in mode (w+b). This file object has no directory entry, no file descriptor, and will be automatically deleted.

58

os.tmpnam()

Removed in Python 3. Returns a unique path for a temporary file.

59

os.ttyname(fd)

Returns 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)

Deletes the file path.

61

os.utime(path, times)

Returns the access and modification times of the file specified by path.

62

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

Outputs the file names in a folder by walking up or down the tree.

63

os.write(fd, str)

Writes a string to file descriptor fd. Returns the length of the string actually written.

64

os.path module

Gets file attribute information.

65

os.pardir()

Get the parent directory of the current directory, displaying the directory name as a string.

Leave a Reply

Your email address will not be published. Required fields are marked *