Python OS File/Directory Methods

Advertisements


The os module provides a big range of useful methods to manipulate files and directories. Most of the useful methods are listed here:

SNMethods with Description
1os.access(path, mode)
Use the real uid/gid to test for access to path.
2os.chdir(path)
Change the current working directory to path
3os.chflags(path, flags)
Set the flags of path to the numeric flags.
4os.chmod(path, mode)
Change the mode of path to the numeric mode.
5os.chown(path, uid, gid)
Change the owner and group id of path to the numeric uid and gid.
6os.chroot(path)
Change the root directory of the current process to path.
7os.close(fd)
Close file descriptor fd.
8os.closerange(fd_low, fd_high)
Close all file descriptors from fd_low (inclusive) to fd_high (exclusive), ignoring errors.
9os.dup(fd)
Return a duplicate of file descriptor fd.
10os.dup2(fd, fd2)
Duplicate file descriptor fd to fd2, closing the latter first if necessary.
11os.fchdir(fd)
Change the current working directory to the directory represented by the file descriptor fd.
12os.fchmod(fd, mode)
Change the mode of the file given by fd to the numeric mode.
13os.fchown(fd, uid, gid)
Change the owner and group id of the file given by fd to the numeric uid and gid.
14os.fdatasync(fd)
Force write of file with filedescriptor fd to disk.
15os.fdopen(fd[, mode[, bufsize]])
Return an open file object connected to the file descriptor fd.
16os.fpathconf(fd, name)
Return system configuration information relevant to an open file. name specifies the configuration value to retrieve.
17os.fstat(fd)
Return status for file descriptor fd, like stat().
18os.fstatvfs(fd)
Return information about the filesystem containing the file associated with file descriptor fd, like statvfs().
19os.fsync(fd)
Force write of file with filedescriptor fd to disk.
20os.ftruncate(fd, length)
Truncate the file corresponding to file descriptor fd, so that it is at most length bytes in size.
21os.getcwd()
Return a string representing the current working directory.
22os.getcwdu()
Return a Unicode object representing the current working directory.
23os.isatty(fd)
Return True if the file descriptor fd is open and connected to a tty(-like) device, else False.
24os.lchflags(path, flags)
Set the flags of path to the numeric flags, like chflags(), but do not follow symbolic links.
25os.lchmod(path, mode)
Change the mode of path to the numeric mode.
26os.lchown(path, uid, gid)
Change the owner and group id of path to the numeric uid and gid. This function will not follow symbolic links.
27os.link(src, dst)
Create a hard link pointing to src named dst.
28os.listdir(path)
Return a list containing the names of the entries in the directory given by path.
29os.lseek(fd, pos, how)
Set the current position of file descriptor fd to position pos, modified by how.
30os.lstat(path)
Like stat(), but do not follow symbolic links.
31os.major(device)
Extract the device major number from a raw device number.
32os.makedev(major, minor)
Compose a raw device number from the major and minor device numbers.
33os.makedirs(path[, mode])
Recursive directory creation function.
34os.minor(device)
Extract the device minor number from a raw device number .
35os.mkdir(path[, mode])
Create a directory named path with numeric mode mode.
36os.mkfifo(path[, mode])
Create a FIFO (a named pipe) named path with numeric mode mode. The default mode is 0666 (octal).
37os.mknod(filename[, mode=0600, device])
Create a filesystem node (file, device special file or named pipe) named filename.
38os.open(file, flags[, mode])
Open the file file and set various flags according to flags and possibly its mode according to mode.
39os.openpty()
Open a new pseudo-terminal pair. Return a pair of file descriptors (master, slave) for the pty and the tty, respectively.
40os.pathconf(path, name)
Return system configuration information relevant to a named file.
41os.pipe()
Create a pipe. Return a pair of file descriptors (r, w) usable for reading and writing, respectively.
42os.popen(command[, mode[, bufsize]])
Open a pipe to or from command.
43os.read(fd, n)
Read at most n bytes from file descriptor fd. Return a string containing the bytes read. If the end of the file referred to by fd has been reached, an empty string is returned.
44os.readlink(path)
Return a string representing the path to which the symbolic link points.
45os.remove(path)
Remove the file path.
46os.removedirs(path)
Remove directories recursively.
47os.rename(src, dst)
Rename the file or directory src to dst.
48os.renames(old, new)
Recursive directory or file renaming function.
49os.rmdir(path)
Remove the directory path
50os.stat(path)
Perform a stat system call on the given path.
51os.stat_float_times([newvalue])
Determine whether stat_result represents time stamps as float objects.
52os.statvfs(path)
Perform a statvfs system call on the given path.
53os.symlink(src, dst)
Create a symbolic link pointing to src named dst.
54os.tcgetpgrp(fd)
Return the process group associated with the terminal given by fd (an open file descriptor as returned by open()).
55os.tcsetpgrp(fd, pg)
Set the process group associated with the terminal given by fd (an open file descriptor as returned by open()) to pg.
56os.tempnam([dir[, prefix]])
Return a unique path name that is reasonable for creating a temporary file.
57os.tmpfile()
Return a new file object opened in update mode (w+b).
58os.tmpnam()
Return a unique path name that is reasonable for creating a temporary file.
59os.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.
60os.unlink(path)
Remove the file path.
61os.utime(path, times)
Set the access and modified times of the file specified by path.
62os.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.
63os.write(fd, str)
Write the string str to file descriptor fd. Return the number of bytes actually written.


Advertisements
Advertisements