Handling Text file in Python

Handling Text file in Python (Writing Data in Text file)

Storing data in text file is one of the important work people use to do in any language. If you will go for C++ and Java, you have to write lots of code for doing and making connection of stream explicitly and have to import many additional libraries to do so.

But here in case of python you no need to worry about all these. You have to write just few line of code to write data into text file. Here is the sample code.

 fout = open("StuTextFile.txt","a+")
name = input("Enter your name : ")
addr = input("Enter address : ")
school = input("Enter school : ")
str = name+"\t"+addr+"\t"+school
fout.write(str)
fout.close()

Code description –

In first line of code a file “StuTextFile.txt” will get open in append mode so that data entered will get saved again and again without overwriting it in to file. As you know default file mode is r+ to read, w+ for write and a+ to append.

Second, third and fourth line is just to get input from user (name, addr, school). Fifth line is creating a string with tab space. Sixth line is the important line to call write() function on the file object created (fout). Last line is to close the file object. It means file object’s connection with file will be disconnected.

0 thoughts on “Handling Text file in Python

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

    • Very true Steve, I used a+ mode to append the contents at the end. Lot more from file handling is to come in this platform. You can add some more value this post with your experience with python.

    • Hello Amanda,
      Thank you for visiting my blog. Will write one post very soon on exception handling with python 2.7 and 3.7 both.

      Keep viewing it.

    • Hello Jonathan,
      List having two methods append() and extend() to append items in it. append() is used to add single item and extend() is to add another compound data like list or tuple or string to existing list.
      list.append(89)
      list.extend([5,6,3,4])
      You can try both the examples.

      Thank you for writing valuable comment.

  • In Python, there is no need for importing external library to read and write files. Python provides an inbuilt function for creating, writing and reading files. Python provides an inbuilt function for creating, writing and reading files.

    • Hello Raymond,
      You are absolutely right. We no need to import any external library to read and write files. Python having inbuild functions.
      Thank you for writing.

    • Hello Jose,
      Defenatally I will write post on exception handling in case of python. Do visit pythonport again.

  • In Python, there is no need for importing external library for file handling. Learn how to create, open, append, read , Read line by line, and Write,

    • Hello Ashlee,
      You are correct. No need to import external library for file handling in python. Python Build-In libraries are very efficient to do so many task. And coding also become very simple in case of file handling if you compare it with other languages.
      Thanks for viewing my website. Appriciate your valuable comment on pythonport.com

  • Get started learning Python with DataCamp’s free Intro to Python tutorial.Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>