Latest web development tutorials

Python3 OS file / directory methods

os module provides a very rich way to deal with files and directories.Commonly used method in the following table:

No. Method and Description
1

os.access (path, mode)


Test mode permission
2

os.chdir (path)


Change the current working directory
3

os.chflags (path, flags)


Mark set the path for the digital signature.
4

os.chmod (path, mode)


Change Permissions
5

os.chown (path, uid, gid)


Change the file owner
6

os.chroot (path)


Change the root directory of the current process
7

os.close (fd)


Close the file descriptor fd
8

os.closerange (fd_low, fd_high)


Close all file descriptors from fd_low (inclusion) to fd_high (not included), the error will be ignored
9

os.dup (fd)


Copy the file descriptor fd
10

os.dup2 (fd, fd2)


Copy a file descriptor fd to another fd2
11

os.fchdir (fd)


By changing the current working directory file descriptor
12

os.fchmod (fd, mode)


Change a file's access rights to the file specified by the parameter fd, the parameter mode is file access permissions on Unix.
13

os.fchown (fd, uid, gid)


Change the ownership of a file, this function modifies a file's user ID and group ID, the file specified by the file descriptor fd.
14

os.fdatasync (fd)


Forced writing file to disk, the file specified by the file descriptor fd, but does not enforce the update file status information.
15

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


Create a file object file descriptor fd, and returns the file object
16

os.fpathconf (fd, name)


Returns an open file system configuration information. Value name to retrieve the system configuration, it may be a string of defined system values, these names specified in many standards (POSIX.1, Unix 95, Unix 98, and others).
17

os.fstat (fd)


Returns the file descriptor fd state, like stat ().
18

os.fstatvfs (fd)


Returns information file contains the file descriptor fd of the file system, such as statvfs ()
19

os.fsync (fd)


Force the file descriptor fd of the file is written to the hard disk.
20

os.ftruncate (fd, length)


Clipping file descriptor fd corresponding file, so it can not exceed the maximum file size.
twenty one

os.getcwd ()


Returns the current working directory
twenty two

os.getcwdu ()


Return Unicode objects in a current working directory
twenty three

os.isatty (fd)


If the file descriptor fd is open, and connected with the tty (-like) device, it returns true, otherwise False.
twenty four

os.lchflags (path, flags)


Mark set the path for the digital signature, similar chflags (), but no soft links
25

os.lchmod (path, mode)


Modify the connection file permissions
26

os.lchown (path, uid, gid)


Change the file owner, similar chown, but do not follow links.
27

os.link (src, dst)


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

os.listdir (path)


Returns a list of files or file folder that contains the specified path of the folder name.
29

os.lseek (fd, pos, how)


Setting the file descriptor fd current position pos, how modify: pos SEEK_SET 0 Setup from the File or the beginning of the calculation; SEEK_CUR or 1 from the current position calculation; os.SEEK_END or two from the end of the file began in unix, Windows. effective
30

os.lstat (path)


Like stat (), but no soft links
31

os.major (device)


Extraction device major number from a raw device number (using stat in st_dev or st_rdev field).
32

os.makedev (major, minor)


In major and minor device number consisting of a number of original equipment
33

os.makedirs (path [, mode])


Recursive folder creation function. Like mkdir (), but all intermediate-level folders that you create needs to include subfolders.
34

os.minor (device)


Extraction device minor number from a raw device number (using stat in st_dev or st_rdev field).
35

os.mkdir (path [, mode])


In digital mode the mode to create a folder named path. The default mode is 0777 (octal).
36

os.mkfifo (path [, mode])


Create a named pipe, mode to digital, the 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 desired options open, mode parameter is optional
39

os.openpty ()


Open a new pseudo-terminal pair. Back pty and tty file descriptor.
40

os.pathconf (path, name)


Returns relevant documents the system configuration information.
41

os.pipe ()


Create a pipe. Returns a pair of file descriptors (r, w) respectively to read and write
42

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


Open a command from a pipeline
43

os.read (fd, n)


Read from the file descriptor fd up to n bytes and returns a string containing the bytes read, the corresponding file descriptor fd has reached the end, returns an empty string.
44

os.readlink (path)


Returns the soft link points to a file
45

os.remove (path)


Remove the path for the path of the file. If the path is a folder, will throw OSError; see below rmdir () deletes a directory.
46

os.removedirs (path)


Recursively delete the directory.
47

os.rename (src, dst)


Rename files or directories from src to dst
48

os.renames (old, new)


Recursively rename the directory, the file can also be renamed.
49

os.rmdir (path)


Remove empty directory path specified, if the directory is not empty, then throw a OSError exception.
50

os.stat (path)


Get path specified information, functionally equivalent to the C API in the stat () system call.
51

os.stat_float_times ([newvalue])
Decision on whether to float objects display the timestamp stat_result

52

os.statvfs (path)


Gets the path of the file system statistics
53

os.symlink (src, dst)


Create a soft link
54

os.tcgetpgrp (fd)


The process returns to the terminal fd (a os.open returned by the open () file descriptor) associated with the group
55

os.tcsetpgrp (fd, pg)


Process group setting terminal fd (consisting os.open returned () open file descriptors) associated to pg.
56

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


Returns a unique pathname for creating temporary files.
57

os.tmpfile ()


It returns an open mode (w + b) file object. This object does not have a file folder entry, no file descriptor will be automatically deleted.
58

os.tmpnam ()


To create a temporary file returns a unique path
59

os.ttyname (fd)


It returns a string that represents the file descriptor fd is associated with a terminal device. If fd is not associated with a terminal device, an exception is thrown.
60

os.unlink (path)


Delete the file path
61

os.utime (path, times)


Back to access and modify the file path specified time.
62

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


Output folder by the file name in the tree walk, up or down.
63

os.write (fd, str)


The string is written to the file descriptor fd. Returns the actual length of the string is written