Working with Files in Python

Working with Files in Python

Python also allows programmers to handle files. It supports reading and writing of files along with several other file handling options. The concept of file handling is short and straightforward in Python. This language treats files as binary or text. Every line of python code includes a sequence of characters. Also, each line of a file code is terminated by a special character called the EOL (End of Line Characters). The EOL includes a comma or a newline character. This character ends the current line and tells the compiler that a new line has begun.

Reading and Writing Files in Python

  • The open() function

In Python, the open function is used to open a file in read or write mode. It is used with two arguments to return a file object. The two arguments accept file mode and name, whether to read or write.

The syntax for opening a file in Python is:

Open (Filename, mode),

Python supports three kinds of modes in file opening. They are:

  • r – For reading
  • w – for writing
  • a – for appending
  • r+ -for both writing and reading

You should remember that the mode argument is not imperative. If you do not pass it, the Python will assume or set it as “r” by default. The program below has been written by our python homework helpers and demonstrate how the read mode works:

# Open a file named “PythonNotes” using the reading mode

file = open ( ‘PythonNotes.txt’, ‘r’)

#Print every line in the file one by one

for each in file:

print (each)

This code will open the file in the read mode and print each line present in the file.

  • How the read () mode works

There are many ways of reading a file in Python. The file.read() function is used to extract a string that contains all characters in the file. Here is how the code would look like:

# Python code demonstrating the read()

File = open (“file.text”, “r”)

Print file.read ()

The other way of reading a file is by calling a certain number of characters. In the sample code below, the interpreter will read the first five characters in the stored data and return it as a string:

# Python code demonstrating the read()

File = open (“file.text”, “r”)

Print file.read (5)

  • How to create a file using the write() mode

Next, we will see how to create a file and how the write mode works. Please write the following in your python environment:

#Python code for creating a file

file = open ( ‘ PythonNotes.txt’, ‘w’)

file.write (“The write command”)

file.write (“This command allows us to write in a particular file”)

file.close ()

The close () command is used to terminate all the resources that are in use. It frees the system of this particular program.

  • How the append () mode works

So how does the append () works? The code below will help you understand it better:

# A Python code demonstrating the append () mode

file = open ( ‘ PythonNotes.txt’, ‘a’)

file.write (“This line will be added by this command”)

file.close ()

Python also has various other commands in file handling that can be used in other tasks. They include:

  • rstrip() – This function removes characters from the right based on the argument
  • lstrip() – It is a function removes characters from the left based on the argument

 

  • How the with () works

The with () in Python is designed to have a much cleaner syntax and exceptions handling when using a code. It is good practice to use it with a statement where applicable. This will be helpful because any files opened will be automatically closed after you are done. It performs an auto-clean-up

Code example:

# Python code that shows how to use the with ()

with open (“file.txt”) as file:

data = file.read ()

  • How to use the write () along with the with ()

We can also use the write function along with the with() function. You can see this in the code below:

#Python code that illustrates the use of write () and with ()

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

f.write (“welcome to Programmingassignmenthelper!”)

  • Split() using file handling

In Python, you can split lines using file handling. This is often done to split the variable when space is encountered. Programmers can also split using characters as they wish. Here is a code example to show you how it is done.

# A Python code that illustrates the split () function

with open ( “file.text”, “r”) as file :

data = file.readlines ()

for line in data:

word = line.split ()

print word

Python has other functions that help manipulate files and their contents. We have not ventured into some of those functions in this article. You can get our help with python assignment if you need immediate assistance or can explore our samples repository.