Skip to content

Dr. Praveen Borra

  • About
  • Expertise
  • Work
  • Research
  • Media
  • Blog
  • Contact
September 21, 2020

·

2–3 minutes

Reading and Writing File in Python

By Dr. Praveen Borra

Reading and Writing Files:

open() returns a file object and is most used with two arguments: open(filename, mode).

f = open(‘logfile’, ‘w’)

The first argument is a string containing the filename. The second argument is another string containing a few characters describing the way in which the file will be used. mode can be ‘r’ when the file will only be read, ‘w’ for only writing (an existing file with the same name will be erased), and ‘a’ opens the file for appending; any data written to the file is automatically added to the end. ‘r+’ opens the file for both reading and writing. The mode argument is optional; ‘r’ will be assumed if it’s omitted.

with keyword when dealing with file objects:

It is good practice to use the with keyword when dealing with file objects. The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point. Using with is also much shorter than writing equivalent try-finally blocks:

with open(‘logfile’) as f:
… read_data = f.read()

Methods of File Objects:

To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string (in text mode) or bytes object (in binary mode). size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, at most size characters (in text mode) or size bytes (in binary mode) are read and returned. If the end of the file has been reached, f.read() will return an empty string (‘’).

f.readline() reads a single line from the file; a newline character (\n) is left at the end of the string and is only omitted on the last line of the file if the file doesn’t end in a newline. This makes the return value unambiguous; if f.readline() returns an empty string, the end of the file has been reached, while a blank line is represented by ‘\n’, a string containing only a single newline.

f.readline()
‘This is the first line of the file.\n’
f.readline()
‘Second line of the file\n’
f.readline()
‘’
If you want to read all the lines of a file in a list, you can also use list(f) or f.readlines().
f.write(string) writes the contents of string to the file, returning the number of characters written.
f.write(‘This is a test\n’)
15

Share this:

  • Share on X (Opens in new window) X
  • Share on Facebook (Opens in new window) Facebook
Like Loading…

Dr. Praveen Borra

Principal Data & AI Architect for Microsoft Fabric and Azure, with a Ph.D. in Computer Science from Florida Atlantic University. I write about cloud, data and AI.

About me · Services · Get in touch

Leave a Reply Cancel reply

Keep reading

Latest articles

  • July 5, 2024

    Edge Computing: Applications Across Industries and Future Innovations

    Introduction In today’s digital landscape, where data generation and consumption are at an all-time high, traditional methods of data processing face significant…

  • July 13, 2022

    Azure SQL Edge

    What is Azure SQL Edge? Azure SQL Edge is an optimized relational database engine geared for IoT and IoT Edge deployments. It…

  • July 13, 2022

    Microsoft Purview

    What’s available in the Microsoft Purview governance portal? Microsoft Purview’s solutions in the governance portal provide a unified data governance service that…

All articles →

Dr. Praveen Borra

Enterprise data and AI leader on Microsoft Fabric and Azure. Ph.D. in Computer Science, Florida Atlantic University.

Dallas, Texas

Get in touch →

Explore

About

Services

Work

Research

Teaching

Media

Blog

Connect

Contact

LinkedIn

YouTube

Google Scholar

All profiles

© 2026 Dr. Praveen Borra

Views expressed on this site are my own.

  • Comment
  • Reblog
  • Subscribe Subscribed
    Dr. Praveen Borra

    Have a WordPress.com account? Log in now.

  • Dr. Praveen Borra
    Copy shortlinkView post in Reader
    Manage subscriptionsSign upLog in
    Report this content
    Collapse this bar
%d