Python Programming/Files

File I/O
Read entire file: In this case the "r" parameter means the file will be opened in read-only mode.

Read certain amount of bytes from a file:

When opening a file, one starts reading at the beginning of the file, if one would want more random access to the file, it is possible to use  to change the current position in a file and   to get to know the current position in the file. This is illustrated in the following example: Here a file is opened, twice ten bytes are read,  shows that the current offset is at position 20, now   is used to go back to position 10 (the same position where the second read was started) and ten bytes are read and printed again. And when no more operations on a file are needed the  function is used to close the file we opened.

Read one line at a time: In this case  will return an array containing the individual lines of the file as array entries. Reading a single line can be done using the  function which returns the current line as a string. This example will output an additional newline between the individual lines of the file, this is because one is read from the file and print introduces another newline.

Write to a file requires the second parameter of  to be "w", this will overwrite the existing contents of the file if it already exists when opening the file:

Append to a file requires the second parameter of  to be "a" (from append): Note that this does not add a line break between the existing file content and the string to be added.

Since Python 2.5, you can use with keyword to ensure the file handle is released as soon as possible and to make it exception-safe:

Or one line at a time:

Related to the with keywords is ../Context Managers/ chapter.

Links:
 * 7.5. The with statement, python.org
 * PEP 343 -- The "with" Statement, python.org

Testing Files
Determine whether path exists:

When working on systems such as Microsoft Windows&trade;, the directory separators will conflict with the path string. To get around this, do the following:

A better way however is to use "raw", or :

But there are some other convenient functions in, where   only confirms whether or not path exists, there are functions which let you know if the path is a file, a directory, a mount point or a symlink. There is even a function  which reveals the true destination of a symlink:

Common File Operations
To copy or move a file, use the shutil library.

To perform a recursive copy it is possible to use, to perform a recursive remove it is possible to use

To remove an individual file there exists the  function in the os module:

Finding Files
Files can be found using glob:

The content of a directory can be listed using listdir:

Getting a list of all items in a directory, including the nested ones:

Above, root takes value of each directory in /user/Joe Hoe including /user/Joe Hoe itself, and directories and files are only those directly present in each root.

Getting a list of all files in a directory, including the nested ones, ending in .txt, using list comprehension:

Links:
 * glob, python.org
 * glob, Py 3, python.org
 * os.listdir, python.org
 * os.walk, python.org
 * os.path.join, python.org

Current Directory
Getting current working directory:

Changing current working directory: