+1 (315) 557-6473 

Python file handling: How to create, open, read, append, and load a text file

An easy and straightforward way to work with files in Python

Working with files homework help

Let’s face it: sometimes, while you are coding, you will need to interact with files on your computer. It does not matter how sophisticated the program is, but the art of reading files into the console is a must-have skill if you want to venture into the programming world. How do you expect to be a data scientist if you cannot load data into the program? Absurd, isn’t it?

 The whole process of file handling is rather complicated and this article cannot find the best way to allocate a sufficient amount of space for all the processes. Instead, we focus on the most crucial part which every programmer has to go through. That is loading files into python. Exclusively, we will focus on the loading of different files such as CSV, text files, Microsoft Excel files, and images. If you have any queries on the 'working with files' topic, please feel free to contact us.


In computing, a file is an object which stores data or other critical information. Examples of the files that will be discussed here are text files and CSV files because they are the ones that are most commonly used.


A directory is a folder in your computer where all the files used in python are stored. It makes it easy for python to manage files as python can easily locate the files in the directory. However, if you have a python directory, it does not imply that the other files in the other folders cannot be retrieved, here you will have to type in the file path instead of the filename. But all in all, they achieve the desired result.

Python’s os module provides us with the methods which we can use to manipulate the files. Examples are the getcwd (), which gets the current working directory, chdir () method that changes the current working directory, and listdir (), which reveals the files and folders in the current directory.

CSV file

A CSV file, as its name might suggest, uses a comma to set apart each value of the data. CSV files are generally easy to work with, no matter what programming language you are using. Python provides different ways of loading a CSV file. You could use inbuilt functions and packages or use external packages such as pandas to load a CSV file. Let’s look at how each method can be applied. Assuming that you have a file named workingexample.csv in your directory, we can now proceed and load the document into python.

 Import CSV

 With open (‘workingxample.csv, ‘r’) as file:

K = csv.reader (file)

For row in k:

Print (row)

 The package CSV is an inbuilt package that is found in your system. You should not worry about installing it.

Using pandas, you have to ensure that it is installed. An example of how to load a CSV file using pandas is shown below.

Import pandas as pd

K = pd.read_csv(“workingexample.csv”)


The head method enables you to view the first five elements of the CSV file stored in the new variable k.

Loading a text file

Loading a text file into python can easily be accomplished by using inbuilt functions open (). There is no need to load a package. The open takes the file object as the input and a mode that permits the various operations that can be performed on the file. Here is an example of how to load an arbitrary text file named workingexample.txt on our python directory.

F= open (‘workingexample.txt’, ‘r’)

The handle that we have above is the reading file, only ‘r’. This is the default file handle. Other file handlers are read and write ‘r+’, write only ‘w’, and write and read ‘w+’

'Read-only' handler only reads the file from the directory, and if the file is not there, it returns an error. The reading and writing handle is for reading and writing files. Again this one raises an error when the file is not in the directory.

The writing handler overwrites the data if the file exists in the directory. If not, it creates another file in the directory. The writing and reading handler functions the same way as the reading handler, except that it allows for the reading of the file.

This article has not covered much about 'file handling', but you can contact us for 'working with files assignment help' and our experts will be able to manifest their prowess to the fullest extent.

Related Blogs