Total Pageviews

Popular posts

 Parameter Details filename the path to your file or, if the file is in the working directory, the filename of your file access_mode a string value that determines how the file is opened buffering an integer value used for optional line buffering

When it comes to storing, reading, or communicating data, working with the files of an operating system is both necessary and easy with Python. Unlike other languages where file input and output requires complex reading and writing objects, Python simplifies the process only needing commands to open, read/write and close the file. This topic explains how Python can interface with files on the operating system.

 File modes

 There are different modes you can open a file with, specified by the mode parameter. These include:

'r' - reading mode. The default. It allows you only to read the file, not to modify it. When using this mode the file must exist.

'w' - writing mode. It will create a new file if it does not exist, otherwise will erase the file and allow you to write to it.

'a' - append mode. It will write data to the end of the file. It does not erase the file, and the file must exist for this mode.

'rb' - reading mode in binary. This is similar to r except that the reading is forced in binary mode. This is also a default choice.

'r+' - reading mode plus writing mode at the same time. This allows you to read and write into files at the same time without having to use r and w.

'rb+' - reading and writing mode in binary. The same as r+ except the data is in binary

'wb' - writing mode in binary. The same as w except the data is in binary.

'w+' - writing and reading mode. The exact same as r+ but if the file does not exist, a new one is made. Otherwise, the file is overwritten.

'wb+' - writing and reading mode in binary mode. The same as w+ but the data is in binary.

'ab' - appending in binary mode. Similar to a except that the data is in binary.

'a+' - appending and reading mode. Similar to w+ as it will create a new file if the file does not exist. Otherwise, the file pointer is at the end of the file if it exists.

'ab+' - appending and reading mode in binary. The same as a+ except that the data is in binary.

 with open(filename, 'r') as f:     f.read() with open(filename, 'w') as f:     f.write(filedata) with open(filename, 'a') as f:     f.write('\\n' + newdata)

                       r r+ w w+ a a+ 

Read              ✔ ✔ ✘ ✔ ✘ ✔

Write             ✘ ✔ ✔ ✔ ✔ ✔ 

Creates file   ✘ ✘ ✔ ✔ ✔ ✔ 

Erases file    ✘ ✘ ✔ ✔ ✘ ✘

Python 3 added a new mode for exclusive creation so that you will not accidentally truncate or overwrite and existing file.

'x' - open for exclusive creation, will raise FileExistsError if the file already exists 'xb' - open for exclusive creation writing mode in binary. The same as x except the data is in binary. 'x+' - reading and writing mode. Similar to w+ as it will create a new file if the file does not exist. Otherwise, will raise FileExistsError. 'xb+' - writing and reading mode. The exact same as 

x+ but the data is binary x x+ 

Read                             ✘ ✔

 Write                          ✔ ✔

 Creates file                ✔ ✔ 

Erases file                  ✘ ✘ 

Initial position Start Start

Allow one to write your file open code in a more pythonic manner:

Python 3.x Version ≥ 3.3 try:    with open("fname", "r") as fout:        # Work with your open file except FileExistsError:    # Your error handling goes here

In Python 2 you would have done something like

Python 2.x Version ≥ 2.0 import os.path if os.path.isfile(fname):    with open("fname", "w") as fout:        # Work with your open file else:    # Your error handling goes here

 Initial position Start Start Start Start End End


No comments

ict note in A/L

3/Technology ict note in A/L/post-list