Code effortlessly

While solving a use-case we often encounter a need to read some data from a file or to save an output to a file (Basic Input and Output Operations). Various file formats have their own specific nuances.

Without further ado, let us dive into the pythonic pool and learn how to read and write various file formats.

  1. Text file

a. Reading a text file :- Text file(.txt) is the most common file format available. We can load a text file into python memory in the following ways:

Code output
Code output

‘open’ function loads the file from file system to python memory. In the example, first parameter to open function is the path to the file and the other is the mode of operation ‘r’ (for read mode). ‘f’ is the object reference. ‘’ this method reads the complete text in the file.

Another way to read text file could be:

b. Writing to a text file: To write to a file we use open command in write ‘w’ mode, if the file does not exist already it creates it. Use append ‘a’ mode if file already has some content in it as ‘w’ mode will overwrite all the data.

with open(‘example.txt’,’w’) as f:
 f.write(‘I learned how to write to a file’)
with open(‘example.txt’,’w’) as f:
 f.write(‘I learned how to write to a file’)

New content in the file:

To add new line to a file using append ‘a’ operation:

New content in the file:

2. CSV file:

a. Reading a csv file: A csv file can be opened as a normal text file. The text will be lines with comma separations. For example:

However, operating csv in this way is not preferred because various column wise or row wise operations become cumbersome (no tabular relationships are maintained).

Reading the csv via pandas library:

Using pandas we can capture the column and row relationship and it also has various built in methods which makes pandas, the widely used library for tabular computations.

b. Writing a csv file: We can create a csv file using open command and writing to file with comma separated lines.

Using pandas we can create a dataframe and write it as a csv:

The file created has following content:

3. Images:

a. Reading an image: Image file can be read using various libraries like PIL ,Opencv etc.

Images are stored in the form of arrays in python memory. If print the ‘img’ object we get the output as following:

b. Saving images: For saving an augmented image we can use ‘save’ method of PIL or ‘imwrite’ method of opencv.

4. Json file:

a. Reading json files: Python has a module ‘json’ to work with json file format. We can use load method of json module to import a json file into python memory. It works as a wrapper around open command.

b. Saving json files: For saving a json to file system, json module uses a method called dump.

I am confident that after going through this article you can now perform basic python I/O operations related to different file formats.

That’s all folks.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store