Python files Handling

Rumman Ansari   Software Engineer   2022-10-12   954 Share
☰ Table of Contents

Table of Content:


Files are used to store huge collection of data and records permanently.
Many applications require large amount of data. In such situation, we need to use some devices such as hard disk, compact disc etc, to store the data.

Need for a Data File

It is a convenient way to deal with large quantities of data.
• To avoid input of data multiple times during program execution.
• To share data between various programs.

Types of files

  • Text files store information in ASCII or Unicode characters. In text file, each line of text is terminated, (delimited) with a special character known as EOL (End of Line) character.
  • Binary files are just files that contain information in the same format in which the information is held in memory, i.e., In binary file, there is no delimiter for a line.
  • CSV (Comma Separated Value) files are a common file format for transferring and storing data. 

File Path

Absolute File Path : It describes how to access a given file or directory starting from the root of the file system.

Relative File Path : It is interpreted from the perspective of your current working directory.


Access modes specify the type of operations to be performed on the opened file.

read(), readline() and readlines() methods are available for reading data from the file.

write() and writelines() are used for writing data in the file.

pickle module is used in serialization of data. This allows us to store data in binary form in the file.

dump and load functions are used to write and read data from file.

file() : This is same as open ().

Random Access :

There are two functions that allow us to access a file in a non-sequential or random mode.

•  tell() : It tells us the position of the file pointer.
seek() : It moves the file pointer to the position specified.

open() function

The open() function creates a file object which would be utilized to call other methods associated with it.

Syntax :

file_object=open(filename[ access_mode],[ buffering])

Here is the parameter details:
filename: The file name argument is a string value that contains the name of the file that you want to access.
access_mode: The access_mode determines the mode in which the file has to be opened i.e., read, write, append, etc. A complete list of possible values is given below in the table. This is optional parameter and the default file access mode is read (r).

File Opening Modes

MODES

DESCRIPTION

r

Opens a file for reading only in text format. The file pointer is placed at the beginning of the file. This is the default mode.

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.

r+

Opens a file for both reading and writing. The file pointer will be at the beginning of the file.

rb+

Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file.

w

Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

wb

Opens a file for writing only in binary format. Overwrites the file if the file 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 the file exists. If the file does not exist, creates a new file for reading and writing.

wb+

Opens a file for both writing and reading in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for reading and writing.

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.

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.

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 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.

Buffering:

If the buffering value is set to 0, no buffering will take place. If the buffering value is 1, line buffering will be performed while accessing a file. If you specify the buffering value as an integer greater than 1, then buffering action will be performed with the indicated buffer size. If negative, the buffer size is the system default (default behaviour).

The file object attributes:

Once a file is opened and you have one file object, you can get various information related to that file. Here is a list of all attributes related to the file object:

ATTRIBUTES

DESCRIPTION

file.closed

Returns True if file is closed, False otherwise.

file.mode

Returns 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.

Functions


(a) read () : syntax: <file handle>.read([n])
It reads at most n bytes and returns the read bytes as string. If `n’ is not specified it reads the entire file.


(b) readline () : syntax: <file handle>.readline ([n])
It reads a line of input, and returns it in the form of a string.


(c) readlines () : syntax: <file handle>.readlines ()
It reads all lines and returns them in a list.


(d) write () : syntax: <filehandle>.write (str1)
It writes string str1 to file referenced by <file handle>


(e) writelines () : syntax: <file handle>.writelines (L).
It writes all strings in list L as lines to file referenced by <file handle>


(f) flush () : syntax: <file object>.flush()
It forces the writing of data on disc that is still pending in output buffer.


(g) Importing sys module lets you read/write from the standard input/output device using sys.stdin.read () and sys.
stdout.write().


(h) split () function splits a line in columns. It returns columns as items of a list.
(i) rename () function is used to rename a file existing on the disk.
syntax: os.remane(<current_file_name>,<new_file_name>)


(j) remove () function is used to delete a file existing on the disk.
syntax: os.remove(<file_name>)


(k) os.path.join () is used to assemble directory names into a path.


(l) os.path.split () is used to split off the last path component.


(m) os.path.splittext() is used to split file name into primary name and extension.


(n) os.path.exists () function checks if a path actually exists.