Python Unit 4
Python Unit 4
File Handling
Python too supports file handling and allows users to
handle files i.e., to read and write files, along with many
other file handling options, to operate on files.
The file handling plays an important role when the data
needs to be stored permanently into the file. A file is a
named location on disk to store related information.
We can access the stored information (non-volatile) after
the program termination.
Types of Files in Python
Text file,
Binary Files,
CSV file
Text file
Text file store the data in the form of characters.
Text file are used to store characters or strings.
Usually we can use text files to store character data
eg: abc.txt
Binary file
Binary file store entire data in the form of bytes.
Binary file can be used to store text, image,
audio and video.
Usually we can use binary files to store binary
data like images,video files, audio files etc
CSV File
CSV (Comma Separated Values) is a simple file format used to store
tabular data, such as a spreadsheet or database.
A CSV file stores tabular data (numbers and text) in plain text. Each
line of the file is a data record.
Each record consists of one or more fields, separated by commas.
The use of the comma as a field separator is the source of the name
for this file format.
For working CSV files in Python, there is an inbuilt module called
csv.
Working of open() function
Before performing any operation (like read or write) on the
file, first we have to open that file. For this we should use
Python's inbuilt function open()
But at the time of open, we have to specify mode, which
represents the purpose of opening file.
We should use open() function to open a file. This function
accepts 'filename' and 'open mode' in which to open the file.
file handler = open("file name", "open mode", "buffering")
f = open(filename, mode)
The File Opening Mode
w
To write data into file. If any data is already present in the file, it would be
deleted and the present data will be stored.
open an existing file for write operation. If the file already contains some
data then it will be overridden. If the specified file is not already available
then this mode will create that file.
r
To read the data form the file. The file pointer is positioned at the
beginning of the file.
open an existing file for read operation. The file pointer is positioned at
the beginning of the file. If the specified file does not exist then we will
get FileNotFoundError. This is default mode.
The File Opening Mode
a
To append data to the file. Appending means adding at the end of
existing data. The file pointer is placed at the end of the file. If the
file does not exist, it will create new for writing data.
open an existing file for append operation. It won't override existing
data.If the specified file is not already avaialble then this mode will
create a new file.
w+
To write and read data a file. The previous data in the file will be
deleted.
To write and read data. It will override existing data.
The File Opening Mode
r+
To read and write data into the file. The previous data in the file will not
be deleted.The file pointer is placed at the beginning of the file.
a+
To append and read data from the file.It wont override existing data.
To append and read of a file. The file pointer will be at the end of the file if
the file exists. If the file does not exist, it creates a new file for reading and
writing.
x
To open a file in exclusive creation mode for write operation. If the file
already exists then we will get FileExistsError.
The File Opening Mode
All the above modes are applicable for text files. If the above
modes suffixed with 'b' then these represents for binary
files.
Eg: rb,wb,ab,r+b,w+b,a+b,xb
f = open("abc.txt","w")
We are opening abc.txt file for writing data.
Working of open() function
Before performing any operation on the file like reading or writing, first,
we have to open that file.
For this, we should use Python’s inbuilt function open() but at the time of
opening, we have to specify the mode, which represents the purpose of
the opening file.
f = open(filename, mode)
a=open("D:\\new.txt","r")
for b in a:
print(b)
The open command will open the file in the read mode and the for loop
will print each line present in the file.
Working of open() function
Working of read() mode
"x" – Create: this command will create a new file if and only if there is no
file already in existence with that name or else it will return an error.
We've now created a new empty text file!
But if you retry the code below – for example, if you try to create a new file
with the same name as you used (if you want to reuse the filename above)
you will get an error notifying you that the file already exists.
Creating a file using write() mode
"w" – Write: this command will create a new text file
whether or not there is a file in the memory with the new
specified name.
It does not return an error if it finds an existing file with the
same name – instead it will overwrite the existing file.
Creating a file using write() mode
The close() command terminates all the resources in use and frees
the system of this particular program.
Creating a file using write() mode
With the code above, whether the file exists or the file doesn't exist in the
memory, you can still go ahead and use that code. Just keep in mind that it
will overwrite the file if it finds an existing file with the same name.
How to Write to a File in Python
There are two methods of writing to a file in Python, which
are:
The write() method:
This function inserts the string into the text file on a single
line.
Based on the file we have created above, the below line of
code will insert the string into the created text file, which is
"myfile.txt.”
file.write("Hello There\n")
How to Write to a File in Python
The writelines() method:
This function inserts multiple strings at the same time. A
list of string elements is created, and each string is then
added to the text file.
Using the previously created file above, the below line of
code will insert the string into the created text file, which is
"myfile.txt.”
f.writelines(["Hello World ", "You are welcome to Fcc\n"])
Working of append() mode
Closing a file
Once we are done with the read/write operations on a file, it is a good
practice to close the file.
Python provides a close() method to do so.
While closing a file, the system frees the memory allocated to it.
The syntax of close() is:
file_object.close()
Here, file_object is the object that was returned while opening the file.
Python makes sure that any unwritten or unsaved data is flushed off
(written) to the file before it is closed.
Hence, it is always advised to close the file once our work is done.
Also, if the file object is re-assigned to some other file, the previous file is
automatically closed.
NumPy Library
What is NumPy?
NumPy is a Python library used for working with arrays.
It also has functions for working in domain of linear algebra,
fourier transform, and matrices.
NumPy was created in 2005 by Travis Oliphant. It is an open
source project and you can use it freely.
NumPy stands for Numerical Python.
NumPy Library
Why Use NumPy?
In Python we have lists that serve the purpose of arrays, but they are
slow to process.
NumPy aims to provide an array object that is up to 50x faster than
traditional Python lists.
The array object in NumPy is called ndarray, it provides a lot of
supporting functions that make working with ndarray very easy.
Arrays are very frequently used in data science, where speed and
resources are very important.
NumPy Library
Why is NumPy Faster Than Lists?
NumPy arrays are stored at one continuous place in memory unlike
lists, so processes can access and manipulate them very efficiently.
This behavior is called locality of reference in computer science.
This is the main reason why NumPy is faster than lists. Also it is
optimized to work with latest CPU architectures.
Which Language is NumPy written in?
NumPy is a Python library and is written partially in Python, but
most of the parts that require fast computation are written in C or
C++.
Creation of One-Dimensional Arrays
One dimensional array contains elements only in one dimension.
In other words, the shape of the NumPy array should contain only
one value in the tuple.
Method 1: First make a list then pass it in numpy.array()
Method 2: fromiter() is useful for creating non-numeric sequence
type array however it can create any type of array. Here we will
convert a string into a NumPy array of characters.
Method 3: arange() returns evenly spaced values within a given
interval.
Method 4: linspace() creates evenly space numerical elements
between two given limits.
Method 1: First make a list then pass it in numpy.array()
Method 2: fromiter()
Method 3: arange()
Method 3: linespace
Re-shaping of an Array
Numpy is the fundamental package for scientific computing
with Python. Numpy is basically used for creating array of n
dimensions.
Reshaping numpy array simply means changing the shape
of the given array, shape basically tells the number of
elements and dimension of array, by reshaping an array we
can add or remove dimensions or change number of
elements in each dimension.
In order to reshape a numpy array we use reshape method
with the given array.
Re-shaping of an Array
Reshaping : 1-D to 2D
Reshaping : 1-D to 3-D
Reshaping N-D to 1-D array
Element-wise Operations on Matrix (Array)
Operation on Matrix :
1. add() :- This function is used to perform element wise matrix addition.
2. subtract() :- This function is used to perform element wise matrix
subtraction.
3. divide() :- This function is used to perform element wise matrix division.
4. multiply() :- This function is used to perform element wise matrix
multiplication.
5. dot() :- This function is used to compute the matrix multiplication, rather
than element wise multiplication.
6. sqrt() :- This function is used to compute the square root of each
element of matrix.
7. sum(x,axis) :- This function is used to add all the elements in matrix.
Optional “axis” argument computes the column sum if axis is 0 and row sum
if axis is 1.
8. “T” :- This argument is used to transpose the specified matrix.
NumPy Array Indexing