Python write to text file

In this post, we will see how to write to a text file.

There are multiple modes in which you can write to text file.

Create txt file(‘x’)

It will open the file in 'X' mode which will create new file and return error in case file already exists.

Here is content of fruits.txt.

$ cat fruits.txt
Apple
Orange
Banana
Pineapple

Write to txt file(‘w’)

It will open the file in 'w' mode which will write to file and will override the content of fruits.txt

$ cat fruits.txt
Mango
Grapes

As you can see, it has overridden content of file which we have read in 'w' mode.

Append to txt file(‘a’)

It will open the file in ‘a’ mode which will append to the file.

$ cat fruits.txt
Mango
Grapes
Lichy
Pomegranate

As you can see Lichy and Pomegranate were appended to the content of fruits.txt.

That’s all about Python write to text file.

Related Posts

  • How to comment out multiple lines in python
    16 October

    How to comment out multiple lines in python

    Table of ContentsMultiple Line Comments in PythonUsing the # characterUsing triple quotesUsing IDEs and Text Editors In this article, we will see about how to comment out multiple lines in python. The concept of comments is present in most programming languages. We use comments for documentation purposes. The compiler ignores them, but the user can […]

  • Get variable name as String in Python
    01 October

    How to get variable name as String in Python

    Table of ContentsWhat are variable names in Python?How to get a variable name as a string in Python?In Python 2.xIn Python 3.x In Python, there are several ways to convert a string to a variable name, but the reverse process is not exactly utilized so often in the programming world. This article focuses on how […]

  • Python print percent sign
    30 September

    Print percentage sign in python

    Table of ContentsIntroductionPrint the Percentage Sign in PythonUsing the %% characterUsing the format() functionUsing the f-string Introduction In this tutorial, we will see how to print percentage sign in python. The modulus operator (%) or the percentage sign has multiple usages in Python. We use this operator to calculate the remainder for the division between […]

  • Print blank line in Python
    23 September

    Print blank line in Python

    Table of ContentsWhat is a blank line in Python?Ways to print blank line in PythonUsing an empty print() functionUsing the print() function with newline characterUsing the print() function with an empty stringUsing the print() function with the sep parameterWays to print multiple blank lines in PythonUsing the print() function with multiple newline charactersUsing the print() […]

  • 17 September

    Write list to CSV in Python

    Table of ContentsUsing the write() function to write a list to a CSV fileUsing the csv module to write a list to a CSV fileUsing the numpy module to write a list to a CSV fileUsing the numpy.savetxt() function to write a list to a CSV fileUsing the numpy.tofile() function to write a list to […]

  • Python add commas to number
    11 September

    Python add commas to number

    Table of ContentsUsing the format() function to add commas to numbers in PythonUsing the fstrings to add commas to numbers in PythonUsing the regular expressions to add commas to numbers in PythonUsing the locale module to add commas to numbers in Python Python allows us to format values to get the final result in our […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.