data handling in python

hemanshi
9 min readJun 26, 2021

--

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

2.The concept of file handling has stretched over various other languages, but the implementation is either complicated or lengthy, but alike other concepts of Python, this concept here is also easy and short.

3. Python treats file differently as text or binary and this is important. Each line of code includes a sequence of characters and they form text file.

4. Each line of a file is terminated with a special character, called the EOL or End of Line characters like comma {,} or newline character.

5. It ends the current line and tells the interpreter a new one has begun. Let’s start with Reading and Writing files.

Working of open() function

We use open () function in Python to open a file in read or write mode. As explained above, open ( ) will return a file object. To return a file object we use open() function along with two arguments, that accepts file name and the mode, whether to read or write. So, the syntax being: open(filename, mode). There are three kinds of mode, that Python provides and how files can be opened:

  • r “, for reading.
  • w “, for writing.
  • a “, for appending.
  • r+ “, for both reading and writing

Working of read() mode

There is more than one way to read a file in Python. If you need to extract a string that contains all characters in the file then we can use file.read(). The full code would work like this:

# Python code to illustrate read() mode

file = open("file.text", "r")

print (file.read())

Another way to read a file is to call a certain number of characters like in the following code the interpreter will read the first five characters of stored data and return it as a string:

# Python code to illustrate read() mode character wise

file = open("file.txt", "r")

print (file.read(5))

Creating a file using write() mode

Let’s see how to create a file and how write mode works:
To manipulate the file, write the following in your Python environment:

# Python code to create a file

file = open('geek.txt','w')

file.write("This is the write command")

file.write("It allows us to write in a particular file")

file.close()

The close() command terminates all the resources in use and frees the system of this particular program.

Working of append() mode

Let’s see how the append mode works:

# Python code to illustrate append() mode

file = open('geek.txt','a')

file.write("This will add this line")

file.close()

There are also various other commands in file handling that is used to handle various tasks like:

rstrip(): This function strips each line of a file off spaces from the right-hand side.
lstrip(): This function strips each line of a file off spaces from the left-hand side.

It is designed to provide much cleaner syntax and exceptions handling when you are working with code. That explains why it’s good practice to use them with a statement where applicable. This is helpful because using this method any files opened will be closed automatically after one is done, so auto-cleanup.
Example:

# Python code to illustrate with()

with open("file.txt") as file:

data = file.read()

# do something with data

Using write along with with() function

We can also use write function along with with() function:

# Python code to illustrate with() alongwith write()

with open("file.txt", "w") as f:

f.write("Hello World!!!")

split() using file handling

We can also split lines using file handling in Python. This splits the variable when space is encountered. You can also split using any characters as we wish. Here is the code:

# Python code to illustrate split() function

with open("file.text", "r") as file:

data = file.readlines()

for line in data:

word = line.split()

print (word)

Working with Files: Basic Syntax

One of the most important functions that you will need to use as you work with files in Python is open(), a built-in function that opens a file and allows your program to use it and work with it.

This is the basic syntax:

💡 Tip: These are the two most commonly used arguments to call this function. There are six additional optional arguments.

irst Parameter: File

The first parameter of the open() function is file, the absolute or relative path to the file that you are trying to work with.

We usually use a relative path, which indicates where the file is located relative to the location of the script (Python file) that is calling the open() function.

For example, the path in this function call:

open("names.txt") # The relative path is "names.txt"

Only contains the name of the file. This can be used when the file that you are trying to open is in the same directory or folder as the Python script, like this:

But if the file is within a nested folder, like this:

Then we need to use a specific path to tell the function that the file is within another folder.

In this example, this would be the path:

open("data/names.txt")

Notice that we are writing data/ first (the name of the folder followed by a /) and then names.txt (the name of the file with the extension).

💡 Tip: The three letters .txt that follow the dot in names.txt is the "extension" of the file, or its type. In this case, .txt indicates that it's a text file.

Second Parameter: Mode

The second parameter of the open() function is the mode, a string with one character. That single character basically tells Python what you are planning to do with the file in your program.

Modes available are:

  • Read ("r").
  • Append ("a")
  • Write ("w")
  • Create ("x")

You can also choose to open the file in:

  • Text mode ("t")
  • Binary mode ("b")

To use text or binary mode, you would need to add these characters to the main mode. For example: "wb" means writing in binary mode.

💡 Tip: The default modes are read ("r") and text ("t"), which means "open for reading text" ("rt"), so you don't need to specify them in open() if you want to use them because they are assigned by default. You can simply write open(<file>).

Why Modes?

It really makes sense for Python to grant only certain permissions based what you are planning to do with the file, right? Why should Python allow your program to do more than necessary? This is basically why modes exist.

Think about it — allowing a program to do more than necessary can problematic. For example, if you only need to read the content of a file, it can be dangerous to allow your program to modify it unexpectedly, which could potentially introduce bugs.

🔸 How to Read a File

Now that you know more about the arguments that the open() function takes, let's see how you can open a file and store it in a variable to use it in your program.

This is the basic syntax:

We are simply assigning the value returned to a variable. For example:

names_file = open("data/names.txt", "r")

I know you might be asking: what type of value is returned by open()?

Well, a file object.

Let’s talk a little bit about them.

File Objects

According to the Python Documentation, a file object is:

An object exposing a file-oriented API (with methods such as read() or write()) to an underlying resource.

This is basically telling us that a file object is an object that lets us work and interact with existing files in our Python program.

File objects have attributes, such as:

  • name: the name of the file.
  • closed: True if the file is closed. False otherwise.
  • mode: the mode used to open the file.

For example:

f = open("data/names.txt", "a")
print(f.mode) # Output: "a"

Now let’s see how you can access the content of a file through a file object.

Methods to Read a File

For us to be able to work file objects, we need to have a way to “interact” with them in our program and that is exactly what methods do. Let’s see some of them.

Read()

The first method that you need to learn about is read(), which returns the entire content of the file as a string.

Here we have an example:

f = open("data/names.txt")
print(f.read())

The output is:

Nora
Gino
Timmy
William

You can use the type() function to confirm that the value returned by f.read() is a string:

print(type(f.read()))# Output
<class 'str'>

Yes, it’s a string!

In this case, the entire file was printed because we did not specify a maximum number of bytes, but we can do this as well.

Here we have an example:

f = open("data/names.txt")
print(f.read(3))

The value returned is limited to this number of bytes:

Nor

❗️Important: You need to close a file after the task has been completed to free the resources associated to the file. To do this, you need to call the close() method, like this:

How to Create a File

If you need to create a file “dynamically” using Python, you can do it with the "x" mode.

Let’s see how. This is the basic syntax:

Here’s an example. This is my current working directory:

If I run this line of code:

f = open("new_file.txt", "x")

A new file with that name is created:

With this mode, you can create a file and then write to it dynamically using methods that you will learn in just a few moments.

💡 Tip: The file will be initially empty until you modify it.

A curious thing is that if you try to run this line again and a file with that name already exists, you will see this error:

Traceback (most recent call last):
File "<path>", line 8, in <module>
f = open("new_file.txt", "x")
FileExistsError: [Errno 17] File exists: 'new_file.txt'

According to the Python Documentation, this exception (runtime error) is:

Raised when trying to create a file or directory which already exist

Creating the connection

To create a connection between the MySQL database and the python application, the connect() method of mysql.connector module is used.

Pass the database details like HostName, username, and the database password in the method call. The method returns the connection object.

The syntax to use the connect() is given below.

  1. Connection-Object= mysql.connector.connect(host = <host-name> , user = <username> , passwd = <password> )

Consider the following example.

Example

  1. import mysql.connector
  2. #Create the connection object
  3. myconn = mysql.connector.connect(host = “localhost”, user = “root”,passwd = “google”)
  4. #printing the connection object
  5. print(myconn)

Output:

<mysql.connector.connection.MySQLConnection object at 0x7fb142edd780>

Here, we must notice that we can specify the database name in the connect() method if we want to connect to a specific database.

Example

  1. import mysql.connector
  2. #Create the connection object
  3. myconn = mysql.connector.connect(host = “localhost”, user = “root”,passwd = “google”, database = “mydb”)
  4. #printing the connection object
  5. print(myconn)

Output:

<mysql.connector.connection.MySQLConnection object at 0x7ff64aa3d7b8>

--

--

No responses yet