Data File Handling
Introduction:
Data Files:
The data files are the files that stores data pertaining to a specific application for later use. the data files can be stores in two ways.
1. Text files
2. Binary Files
1.Text files:
l A text files stores information in the form of a stream of ASCII or UNICODE characters(the one which is default for your programming platform).
l In text file,each line of text is terminated (delimited) with a special character (as per the operating system) known as EOL(End Of Line)Characters.
l The text files,some internal translation take place when this EOL character is read or written.
l In python,by default,this EOL character is the newline character(‘\n’) carriage-return(‘\r’),newline combination(‘\r\n’).
I) Regular text file
These are the text files which stores the text in the same form as typed.
Here the newline characters ends a line and text translations take place these files
have a file extensions as .txt
II) Delimited Text file
In these text files, a specific character is stored to separate the values.i.e., after each values e.g a tab or a comma after every value
One Two --> TSV file
One,Two,Three --> CSV file
When a tab character is used to separate the values stored,these are called TSV files(Tab Separated Values) .These files can take extension as .txt or .csv
When the comma is used to separate the values stored,these are called CSV files(comma separated values files) these take extension as .csv
For Example
1. Regular text file I am simple text
2. TSV files I am simple text
3. CSV file I,am,simple,text
Some setup files(e.g INI files) and rich text format files(.RTF files) are also text files.
2.Binary Files
A binary files stores in the form of a stream of BYTES
8bit=1 Byte
4 bit= 1 nibble
Binary File extension
.dat
Opening And Closing Files
You need to open it in a specific mode as per the file manipulation task you want to perform.the most basic file manipulation tasks include adding,modifying or deleting data in a file.which in turn any one or combination of the following operations.
1. Reading data from file
2. Writing data from file
3. Appending data from file
Open:
In data file handling through python,the first thing that you do is open the file.it is done using open() function as per one of the following.
Syntax:
<file_objectname>=open(<filename>)
<file_objectname>=open(<filename>,<mode>)
For example:
myfile=open(“test.txt”)
Read mode as a default mode
File Access mode:
1. Read mode -->r
2. Write mode-->w
3. Append mode-->a
myfile1=open(“test.txt”,”r”)
myfile1=open(“test.txt”,”w”)
myfile1=open(“test.txt”,”a”)
File1=open(“E:\\main\\result.txt”,”w”)
NOTE:A FILE_OBJECT IS ALSO KNOWN AS FILE-HANDLE.
f1=open(r“c:\temp\data.txt”,”r”)
Note: the prefix r in front of a string makes it raw string that means there is no special meaning attached to any character.
File Access mode:
1.Read mode -->r
2.Write mode-->w
3.Append mode-->a
Text File Mode | Binary File Mode | Description | Notes |
‘r’ | ‘rb’ | Read only | Default mode: File must exist already,otherwise python raise I/O error. |
‘w’ | ‘wb’ | Write only | l If the file does not exist,file is created. l If the file exits,python will truncate existing data and over-write in the file.so this mode must be used with caution. |
‘a’ | ‘ab’ | Append only | l File is in write only mode l If the file exists, the data in the file is retained and new date being written will be appended to the end. l If the file does not exist,python will create a new file. |
‘r+’ | ‘rb+’, ’r+b’ | Read and write | l File must exist otherwise error is raised. l Both reading and writing operations can take place. |
‘w+’ | ‘wb+’, ‘w+b’ | Write and read | l File is created if does not exists. l If file exists,file is truncated (past data is lost). l Both reading and writing operations can take place. |
‘a+’ | ‘ab+’, ‘a+b’ | Write and read | l File is created if does not exists. l If file exists,files existing data is retained;new data is appended. l Both reading and writing operations can take place. |
NOTE: adding a ‘b’ to text-file mode makes it binary-file mode.
A file_mode governs the type of operations(eg., read/write/append)possible in the opened file i.e ., it refers to how the file will be used once its opened.
Closing File:
An open file is closed by calling close() method of its file_object.closing of file is important.
In python,files are automatically closed at the end of the program but it is good practice to get into the habit of closing your files explicitly.
Syntax:
<file_object>.close()
f1.close()
0 Comments