Text Files
Text Files
A text file is simply a sequence of ASCII or Unicode characters. Text Files A file whose contents can be viewed using a text editor is called a text file. A text file is simply a sequence of ASCII or Unicode characters.
Python programs, contents written in text editors are some of the example of text files. Python programs, contents written in text editors are some of the example of text files.
Binary FilesA binary file stores the data in the same way as as stored in the memory. The .exe files, mp3 file, image files, word documents are Binary FilesA binary file stores the data in the same way as as stored in the memory. The .exe files, mp3 file, image files, word documents are
some of the examples of binary files. We can’t read a binary file using a text editor. some of the examples of binary files. We can’t read a binary file using a text editor.
DIFFERENCE BETWEEN TEXT FILE AND BINARY FILE DIFFERENCE BETWEEN TEXT FILE AND BINARY FILE
Text File Its Bits represent character. Less prone to get corrupt as change soon as made and can be undone. Store only plain text in a file. Widely Text File Its Bits represent character. Less prone to get corrupt as change soon as made and can be undone. Store only plain text in a file. Widely
used file format and can be opened in any text editor. Mostly .txt and .rtf are used as extensions to text files. used file format and can be opened in any text editor. Mostly .txt and .rtf are used as extensions to text files.
Binary File It represents a custom data. Can easily get corrupted, corrupt on reflects as even single bit change. Can store different types of data. Binary File It represents a custom data. Can easily get corrupted, corrupt on reflects as even single bit change. Can store different types of data.
Developed for an application and can be opened in that only. Can have any application defined extensions. Developed for an application and can be opened in that only. Can have any application defined extensions.
Opening To perform file operation, it must be opened first then after reading, writing, editing operation can be performed. To create any new file Opening To perform file operation, it must be opened first then after reading, writing, editing operation can be performed. To create any new file
then too it must be opened. On opening of any file, a file relevant structure is created in memory as well as memory space is created to store then too it must be opened. On opening of any file, a file relevant structure is created in memory as well as memory space is created to store
contents. Once we are done working with the file, we should close the file. contents. Once we are done working with the file, we should close the file.
Closing Files Closing a file releases valuable system resource. In case we forgot to close the file, Python automatically close the file when program Closing Files Closing a file releases valuable system resource. In case we forgot to close the file, Python automatically close the file when program
ends or file object is no longer referenced in the program. However, if our program is large and we are reading or writing multiple files that can take ends or file object is no longer referenced in the program. However, if our program is large and we are reading or writing multiple files that can take
significant amount of resource on the system. If we keep opening new files carelessly, we could run out of resources. So be a good programmer, significant amount of resource on the system. If we keep opening new files carelessly, we could run out of resources. So be a good programmer, close
close the file as soon as all task are done with it. the file as soon as all task are done with it.
opening modes Sr. No Mode & Description opening modes Sr. No Mode & Description
1. r - reading only. Sets file pointer at beginning of the file. This is the default mode. 11. r - reading only. Sets file pointer at beginning of the file. This is the default mode.
2. rb – same as r mode but with binary file. 12. rb – same as r mode but with binary file.
3. r+ - both reading and writing. The file pointer placed at the beginning of the file. 13. r+ - both reading and writing. The file pointer placed at the beginning of the file.
4. rb+ - same as r+ mode but with binary file. 14. rb+ - same as r+ mode but with binary file.
5. w - writing only. Overwrites the file if the file exists. If not, creates a new file for writing. 15. w - writing only. Overwrites the file if the file exists. If not, creates a new file for writing.
6. wb – same as w mode but with binary file. 16. wb – same as w mode but with binary file.
7. w+ - both writing and reading. Overwrites. If no file exists, creates a new file for R & W. 17. w+ - both writing and reading. Overwrites. If no file exists, creates a new file for R & W.
8. wb+ - same as w+ mode but with binary file. 18. wb+ - same as w+ mode but with binary file.
9. a -for appending. Move file pointer at end of the file.Creates new file for writing,if not exist. 19. a -for appending. Move file pointer at end of the file.Creates new file for writing,if not exist.
10. ab – same as a but with binary file 20. ab – same as a but with binary file
open() Function The key function for working with files in Python is the open() function. The open() function takes two parameters; filename, and open() Function The key function for working with files in Python is the open() function. The open() function takes two parameters; filename, and
mode. There are four different methods (modes) for opening a file: "r" - Read - Default value. Opens a file for reading, error if the file does not exist` mode. There are four different methods (modes) for opening a file: "r" - Read - Default value. Opens a file for reading, error if the file does not exist`
"w" - Write - Opens a file for writing, creates the file if it does not exist "x" - Create - Creates the specified file, returns an error if the file exists In "w" - Write - Opens a file for writing, creates the file if it does not exist "x" - Create - Creates the specified file, returns an error if the file exists In
addition you can specify if the file should be handled as binary or text mode "t" - Text - Default value. Text mode "b" - Binary - Binary mode (e.g. addition you can specify if the file should be handled as binary or text mode "t" - Text - Default value. Text mode "b" - Binary - Binary mode (e.g.
images) images)
CSV FILE (Comma separated value)is a file format for data storage which looks like a text file. The information is organized with one record on each CSV FILE (Comma separated value)is a file format for data storage which looks like a text file. The information is organized with one record on each
line and each field is separated by comma. line and each field is separated by comma.
CSV File Characteristics • One line for each record • Comma separated fields • Space-characters adjacent to commas are ignored • Fields with in- CSV File Characteristics • One line for each record • Comma separated fields • Space-characters adjacent to commas are ignored • Fields with in-
built commas are separated by double quote characters built commas are separated by double quote characters
When Use CSV? • When data has a strict tabular structure • To transfer large database between programs • To import and export data to office When Use CSV? • When data has a strict tabular structure • To transfer large database between programs • To import and export data to office
applications, Qedoc modules • To store, manage and modify shopping cart catalogue applications, Qedoc modules • To store, manage and modify shopping cart catalogue
CSV Advantages • CSV is faster to handle • CSV is smaller in size • CSV is easy to generate • CSV is human readable and easy to edit manually • CSV CSV Advantages • CSV is faster to handle • CSV is smaller in size • CSV is easy to generate • CSV is human readable and easy to edit manually • CSV
is simple to implement and parse • CSV is processed by almost all existing applications is simple to implement and parse • CSV is processed by almost all existing applications
CSV Disadvantages • No standard way to represent binary data • There is no distinction between text and numeric values • Poor support of CSV Disadvantages • No standard way to represent binary data • There is no distinction between text and numeric values • Poor support of special
special characters and control characters • CSV allows to move most basic data only. Complex configurations cannot be imported and exported this characters and control characters • CSV allows to move most basic data only. Complex configurations cannot be imported and exported this way •
way • Problems with importing CSV into SQL (no distinction between NULL and quotes) Problems with importing CSV into SQL (no distinction between NULL and quotes)
Write / Read CSV FILE Writing and reading operation from text file is very easy. First of all, we have to import csv module for file Write / Read CSV FILE Writing and reading operation from text file is very easy. First of all, we have to import csv module for file operation/method
operation/method call. For writing, we open file in ‘w’ writing mode using open () method which create newFile like object. Through csv.writer() call. For writing, we open file in ‘w’ writing mode using open () method which create newFile like object. Through csv.writer() method ,we create
method ,we create writer object to call writerow() method to write objects. Similarly for reading, we open the file in ‘r’ mode and create newFile like writer object to call writerow() method to write objects. Similarly for reading, we open the file in ‘r’ mode and create newFile like object,further we
object,further we create newfilereader object using csv.reader() method to read each row of the file. Three file opening modes are there ‘w’,’r’,’a’. ‘a’ create newfilereader object using csv.reader() method to read each row of the file. Three file opening modes are there ‘w’,’r’,’a’. ‘a’ for append After
for append After file operation close, opened file using close () method. file operation close, opened file using close () method.
.
to illustrate read() function by reading the entire data from file to illustrate read(n) function by reading the first 10 character from file
(test.txt) (test.txt)
with open('test.txt', 'r') as file: with open('test.txt', 'r') as file:
data = file.read() data = file.read(10)
print(data) print(data)
to read data by line using readline() method. to read all the lines from the file into list.
file = open('your_file.txt', 'r') with open('filename.txt', 'r') as file:
line = file.readline() lines = file.readlines()
while line:
print(line.strip())
line = file.readline()
file.close()
to display the content of the file starting from 3rd index character into Reading form a file:
read(): To read the entire data from the file; starts reading from the
the list. cursor up to the end of the file.
with open('filename.txt', 'r') as file: readline(): To read only one line from the file; starts reading from the
cursor up to, and including, the end of line character.
lines = file.readlines() readlines(): To read all lines from the file into a list; starts reading from
the cursor up to the end of the file and returns a list of lines.
content = [line[2:] for line in lines] Writing to the text file:
1. write( ): This method takes a string as a
parameter and writes it in the file. As we
discussed about the file mode earlier, we use
‘w’ access mode to write date to the text file.
Syntax: fileObject.write (string)
2. writelines ( ): This method is used whenever
we have to write a sequence of data types or
string.
FunctionsPython provides the features to divide a large program into different smaller modules, called Functions. A Function is a group of statements i.e. subprogram, that TOKENS IN PYTHONThe smallest individual unit in a program is called a token.
exists within a program for the purpose of performing specific tasks, and returns values/results whenever it is required.Functions can take some data as input, process upon TYPES OF TOKEN (i) Keyword (ii) Identifiers (iii) Literals (iv) Operators & (v) Punctuators
according to the instructions, and give some output at the end. Example – max( ), min( ), sum( ), type(), etc. Keyword The word has a special meaning in a programming language. If, else, while, for, or etc
Types of FunctionsIn Python, Functions can be categorized into three categoriesBuilt-in functions Functions defined in Modules User-defined Functions Identifiers Names that are given to different parts of programs. Variable, Function, Class, etc.
(a) Built-in FunctionsBuilt-in Functions are the predefined functions that are already available in Python. These functions are always available in the standard library, no Literals Data items that have a fixed value. Several types of literal in Python. (a) String (b) Numeric (c) Boolean (d) None
need to import any module for them. Operators Triggers some computation or action when applied to the variables. +, -, / .
Most frequently used Built-in Functions 1.int( ) – to convert into an integer. 2.float ( ) – to convert into float. 3.bool ( ) – to convert into Boolean. 4.str ( ) – Punctuators Symbols that are used to organize sentence, structures and expressions. ‘ “ # \ ( ) { } @ , : ; ` =, etc
to convert into a string. 5.list ( ) – to convert a sequence into a list. 6.tuple( ) – to convert a sequence into a tuple. 7.dict( ) – to convert sequences into the VARIABLES AND ASSIGNMENTS A Variable is a named memory location, used for storing and manipulating values.
dictionary. 8.input( ) – to input value as a string. 9.eval( ) – to evaluate strings in the form of expression. 10.print( ) – to print/show output. DYNAMIC TYPING A variable pointing to a value of a certain type can be made to point to a value/object of a different type. This is called Dynamic
11.range( ) – to define a series of numbers, useful for a loop. 12.max( ) – to find the maximum numbers. 13.min ( ) – to find the minimum numbers. Typing.
14.sum( ) – to sum two numbers or a list or tuple. 15.abs( ) – to find the absolute value (i.e. positive number) of numbers. 16.round (n, p ) – rounds a given Integers Two types of integers in Python a) Integer (signed) – number having only integer, either positive or negative b) Booleans –
floating point number to the specified number of digits and returns the result. 17.len( ) – returns the length of the sequence. 18.chr( ) – returns the represent truth values False and True. Boolean values False and True behave like values 0 and 1, respectively.
equivalent ASCII character of the given number. 19.ord( ) – returns the ordinal value (ASCII number) of the given character. 20.type ( ) – returns the data type of Floating Point Numbers Is used to store numbers with a fraction part. They can be represented in scientific notation. Also called real numbers.
data or variable. 20.id( ) – returns the memory reference of value or variable. Complex NumbersPython allows to store the complex number. Complex numbers are pairs of real and imaginary numbers. They can store in the
(b) Functions defined in ModuleThese functions are pre-defined in particular modules and can only be used when the corresponding module is imported.For example, form of ‘a+bj . Both real and imag are internally represented as float value.
1.randint() function of the random module. 2.pow() function of the math module, 3.mean() module of the statistics module, 4.dump() function of Sequence Data Types Sequence Data Types is an ordered collection of items, indexed by integers (both +ve or –ve). Three types of sequence are –
the pickle module, 5.reader() function of csv module, 6.connect() function of mysql.connector module. StringsThe string is a combination of letters, numbers, and special symbols. In Python 3.x string is a sequence of Pure Unicode characters. Unicode is
Calling / Invoking a FunctionA function definition defines user-defined functions. The function definition does not execute the function body; this gets executed only when a system, designed to represent every character from every language. A string is a sequence of characters and each character can be individually
the function is called or invoked.Calling of function means, invoking or accessing or using the function. To execute a function, invoke a function by its name and parameters accessed using its index position i.e. forward (0,1, 2, …) and backward (-1, -2, -3, .. …)
inside the parenthesis (), if required. Syntax: 1.Function_Name( ) 2.Function_Name (arguments or parameters) 3.Res = Function_Name( ) 4.Res = Lists The list is a set of comma-separated values of any datatype inside the square brackets. [ ]
Function_Name( arguments / parameters) Tuples A Tuple is a set of comma-separated values of any data type inside the parentheses ( )
Arguments: Values being passed by function call statement, are called arguments. Also called actual parameter or actual argument Dictionary A dictionary is a comma-separated key-value pairs within { } and accessed using keys.No two keys can be the same .
Parameters: Values being received by function definition statement, are called parameters. Also called formal parameter and formal argument. Mutable and Immutable Data Types
Positional Arguments When the function call statement must match the number and order of arguments defined in the function definition, such types of arguments are Immutable Types Immutable types are those that can never change their value in place.The following are immutable: integers, floating point
called Positional Arguments / Required Arguments / Mandatory Arguments.The process of matching arguments is called Positional Argument Matching. numbers, Boolean, strings, tuples. In immutable types, the variable names are stored as a reference to a value object, each time when we change the
Default ArgumentsPython allows us to assign default values(s) to a function’s parameter(s) which becomes useful when matching arguments is not passed in the function value, the variable’s reference memory address changes.
call statement. Mutable Types The Mutable types are those whose values can be changed in place. The following are mutable : lists, dictionaries, and sets
Keyword (Named) ArgumentThe default arguments allow to specify the default value for the parameters, but it does not allow to pass the value by changing order i.e. ExpressionIn Python, An expression is a valid combination of operators, literals, and variables.There are three types of Expression in Python, These
arguments always passed in the same order. Keyword arguments are the named arguments with assigned values being passed in the function call statement. Keyword are-
argument allows us to write any argument in any order with the help of an argument name. Using keyword arguments, we can pass argument values by keyword i.e. by •Arithmetic Expressions:- combination of integers, floating-point numbers, complex numbers, and arithmetic operators. Example; 2+9, 9.2/3
parameter name. •Relational Expression :- combination of literals and/or variables of any valid type and relational operators . Example; x > y, 5 < 8, z == y
Variable length argumentsPython allows us to pass variable number of arguments to a function, this is called Variable length argument.Variable length arguments are •Logical Expression:- combination of literals, variables and logical operators. Example; t and q, p or g
declared with * (asterisk) symbol. •String Expression :- combination of string literals and string operators ( + -> concatenate, and * -> replicate).
Non – Void FunctionsThe Functions that return some value / values as a result, is called as Non-void Functions. Functions can return value with the help Global Statement In Python, global statement allows us to use the same global variable inside the function. It does not allow functions to create
of return statement. return is a Python keyword. Functions returning a value are also known as Fruitful Functions. another variable of the same name. It means, listed identifiers are part of global namespace or global environment. Python uses LEGB namespace rule
Void FunctionsThe Functions that does not return any value, as a result, is called as Void Functions. In this case no need to write return statement or you many write to search the identifier variable. global <variable name>
return statement without value. Local Environment First check-in the local namespace or local function or local environment, found then use it, otherwise move to Enclosing
Function Returning Multiple ValuesThe Functions that return multiple values i.e. more than one value are called Multi–Value returning Functions. These are some environment.
examples of functions returning a single value. Enclosing Evironment If not found in Local, then it checks inside the enclosing namespace, if found then ok, otherwise moves to the global
Scope of a Variable Scope means the accessibility of variables in other parts of the program. The scope rules of Python, decide in which parts of the program a particular namespace.
variable can be accessed or not. Global Environment If not found in Enclosing, it checks inside the global namespace, if found then ok, otherwise move to the Built-in namespace.
Global Scope:A variable that is defined in the ‘main’ program, is called Global Variable and it has global scope. Built-in Environment If not found in Global, it checks inside the Built-in namespace, if found then ok, otherwise raises an Error message –
Local ScopeA variable that is defined in the function called Local Variable and it has local scope. NameError: name ‘varName’ is not defined.
LifetimeThe time for which a variable remains in memory is called Lifetime of Variable. It depends on the scope of the variable. Local ScopeA variable that is defined in the function called Local Variable and it has local scope.
LifetimeThe time for which a variable remains in memory is called Lifetime of Variable. It depends on the scope of the variable.
to illustrate read() function by reading the entire data from file to illustrate read(n) function by reading the first 10 character from file
(test.txt) (test.txt)
with open('test.txt', 'r') as file: with open('test.txt', 'r') as file:
data = file.read() data = file.read(10)
print(data) print(data)
to read data by line using readline() method. to read all the lines from the file into list.
file = open('your_file.txt', 'r') with open('filename.txt', 'r') as file:
line = file.readline() lines = file.readlines()
while line:
print(line.strip())
line = file.readline()
file.close()
to display the content of the file starting from 3rd index character into Reading form a file:
read(): To read the entire data from the file; starts reading from the
the list. cursor up to the end of the file.
with open('filename.txt', 'r') as file: readline(): To read only one line from the file; starts reading from the
cursor up to, and including, the end of line character.
lines = file.readlines() readlines(): To read all lines from the file into a list; starts reading from
the cursor up to the end of the file and returns a list of lines.
content = [line[2:] for line in lines] Writing to the text file:
1. write( ): This method takes a string as a
parameter and writes it in the file. As we
discussed about the file mode earlier, we use
‘w’ access mode to write date to the text file.
Syntax: fileObject.write (string)
2. writelines ( ): This method is used whenever
we have to write a sequence of data types or
string.