Python Filesystem Reference

Quick reference to the filesystem functions and methods of Python.

File atributes
Attribute Means what
file.closed Returns true if file is closed, false otherwise.
file.mode Returns the access mode with which file was opened.
file.name Returns name of the file.
file.softspace Returns false if space explicitly required with print, true otherwise.
File open mode
Mode Means what
a Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
ab Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
ab+ Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
r+ Opens a file for both reading and writing. The file pointer is placed at the beginning of the file.
rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
rb+ Opens a file for both reading and writing in binary format. The file pointer is placed at the beginning of the file.
w Opens a file for writing only. Overwrites the file if it exists. If the file does not exist, creates a new file for writing.
w+ Opens a file for both writing and reading. Overwrites the file if it exists. If the file does not exist, creates a new file for reading and writing.
wb Opens a file for writing only in binary format. Overwrites the file if it exists. If the file does not exist, creates a new file for writing.
wb+ Opens a file for both writing and reading in binary format. Overwrites the file if it exists. If the file does not exist, creates a new file for reading and writing.
File methods
Method Does what
file.close() Close the file. A closed file cannot be read or written.
file.flush() Flush the internal buffer. This may be unavailable for some file-like objects.
file.fileno() Returns the integer file descriptor that is used to request I/O operations from the operating system.
file.isatty() Returns True if the file is connected to a TTY(-like) device, else False.
file.next() Returns the next line from the file each time it is being called.
file.read([size]) Reads at most size bytes from the file (less if the read hits EOF before obtaining size bytes).
file.readline([size]) Reads one entire line from the file. A trailing newline character is kept in the string.
file.readlines([size]) Reads until EOF using readline() and return a list containing the lines. If the optional size argument is present, instead of reading up to EOF, whole lines totalling approximately size bytes (possibly after rounding up to an internal buffer size) are read.
file.seek(offset[, whence]) Sets the file's current position.
file.tell() Returns the file's current position.
file.truncate([size]) Truncates the file's size. If the optional size argument is present, the file is truncated to (at most) that size.
file.write(str) Writes a string to the file. There is no return value.
file.writelines(sequence) Writes a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings.
OS functions and methods
Function Does what
os.access(path, mode) Uses the real uid/gid to test for access to path.
os.chdir(path) Changes the current working directory to path.
os.chflags(path, flags) Sets the flags of path to the numeric flags.
os.chmod(path, mode) Changes the mode of path to the numeric mode.
os.chown(path, uid, gid) Changes the owner and group id of path to the numeric uid and gid.
os.chroot(path) Changes the root directory of the current process to path.
os.close(fd) Closes file descriptor fd.
os.closerange(fd_low, fd_high) Closes all file descriptors from fd_low (inclusive) to fd_high (exclusive), ignoring errors.
os.dup(fd) Returns a duplicate of file descriptor fd.
os.dup2(fd, fd2) Duplicates file descriptor fd to fd2, closing fd2 first if necessary.
os.fchdir(fd) Changes the current working directory to the directory represented by the file descriptor fd.
os.fchmod(fd, mode) Changes the mode of the file given by fd to the numeric mode.
os.fchown(fd, uid, gid) Changes the owner and group id of the file given by fd to the numeric uid and gid.
os.fdatasync(fd) Forces write of file with filedescriptor fd to disk.
os.fdopen(fd[, mode[, bufsize]]) Returns an open file object connected to the file descriptor fd.
os.fpathconf(fd, name) Returns system configuration information relevant to an open file. Name specifies the configuration value to retrieve.
os.fstat(fd) Returns status for file descriptor fd, like stat().
os.fstatvfs(fd) Returns information about the filesystem containing the file associated with file descriptor fd, like statvfs().
os.fsync(fd) Forces write of file with filedescriptor fd to disk.
os.ftruncate(fd, length) Truncates the file corresponding to file descriptor fd, so that it is at most length bytes in size.
os.getcwd() Returns a string representing the current working directory.
os.getcwdu() Returns a Unicode object representing the current working directory.
os.isatty(fd) Returns True if the file descriptor fd is open and connected to a TTY(-like) device, else False.
os.lchflags(path, flags) Sets the flags of path to the numeric flags, like chflags(), but do not follow symbolic links.
os.lchmod(path, mode) Changes the mode of path to the numeric mode.
os.lchown(path, uid, gid) Changes the owner and group id of path to the numeric uid and gid. This function will not follow symbolic links.
os.link(src, dst) Creates a hard link pointing to src named dst.
os.listdir(path) Returns a list containing the names of the entries in the directory given by path.
os.lseek(fd, pos, how) Sets the current position of file descriptor fd to position pos, modified by how.
os.lstat(path) Like stat(), but do not follow symbolic links.
os.major(device) Extracts the device major number from a raw device number.
os.makedev(major, minor) Composes a raw device number from the major and minor device numbers.
os.makedirs(path[, mode]) Recursive directory creation.
os.minor(device) Extracts the device minor number from a raw device number.
os.mkdir(path[, mode]) Creates a directory named path with numeric mode mode.
os.mkfifo(path[, mode]) Creates a FIFO (a named pipe) named path with numeric mode mode. The default mode is 0666 (octal).
os.mknod(filename[, mode=0600, device]) Creates a filesystem node (file, device special file or named pipe) named filename.
os.open(file, flags[, mode]) Opens the file file and set various flags according to flags and possibly its mode according to mode.
os.openpty() Opens a new pseudo-terminal pair. Return a pair of file descriptors (master, slave) for the PTY and the TTY, respectively.
os.pathconf(path, name) Returns system configuration information relevant to a named file.
os.pipe() Creates a pipe. Return a pair of file descriptors (r, w) usable for reading and writing, respectively.
os.popen(command[, mode[, bufsize]]) Opens a pipe to or from command.
os.read(fd, n) Reads at most n bytes from file descriptor fd. Returns a string containing the bytes read. If the end of the fd file has been reached, an empty string is returned.
os.readlink(path) Returns a string representing the path to which the symbolic link points.
os.remove(path) Removes the file path.
os.removedirs(path) Removes directories recursively.
os.rename(src, dst) Renames the file or directory src to dst.
os.renames(old, new) Recursive directory or file renaming function.
os.rmdir(path) Removes the directory path.
os.stat(path) Performs a stat system call on the given path.
os.stat_float_times([newvalue]) Determines whether stat_result represents time stamps as float objects.
os.statvfs(path) Performs a statvfs system call on the given path.
os.symlink(src, dst) Creates a symbolic link pointing to src named dst.
os.tcgetpgrp(fd) Returns the process group associated with the terminal given by fd (an open file descriptor as returned by open()).
os.tcsetpgrp(fd, pg) Sets the process group associated with the terminal given by fd (an open file descriptor as returned by open()) to pg.
os.tempnam([dir[, prefix]]) Returns a unique path name that is reasonable for creating a temporary file.
os.tmpfile() Returns a new file object opened in update mode (w+b).
os.tmpnam() Returns a unique path name that is reasonable for creating a temporary file.
os.ttyname(fd) Returns 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.
os.unlink(path) Removes the file path.
os.utime(path, times) Sets the access and modified times of the file specified by path.
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]]) Generates the file names in a directory tree by walking the tree either top-down or bottom-up.
os.write(fd, str) Writes the string str to file descriptor fd. Returns the number of bytes written.

More: