How to Create an Empty Text File in Python?

Hi Guys,
Now, let's see post of python create empty text file. I explained simply step by step how to create an empty text file in python. you will learn python make empty txt file. it's simple example of python create empty text file in directory. You just need to some step to done python empty a text file.
A new empty text file can be created in Python in a few different ways. To create a blank text file, we'll utilise the functions open() and close(). I'll give you a few examples of how to make a new text file that is empty in Python. So let's go over each example one by one.
So let's see bellow example:
Example 1:Example 1: Python Empty Create Text File
We shall employ the open() function in this example. It requires two parameters, the first for the filename and the second with the letter "w" for opening a file for writing. then we just use the close() function to close the file. notice the output from the example below:
main.py# create new empty text file code open('readme.txt', 'w').close() print("New empty text file created successfully!")Output
It will create readme.txt file with without text.
Example 2:Python Create Text File
We shall employ the open() function in this example. It requires two parameters, the first for the filename and the second with the letter "w" for opening a file for writing. then we'll write text to a file using the write() function. notice the output from the example below:
main.py# create a new text file code with open('readme.txt', 'w') as f: f.write('New text file content line!') print("New text file created successfully!")Output
It will create readme.txt file with following text.
New text file content line!
Example 3:Python Create Multiline Text File
We shall employ the open() function in this example. It requires two parameters, the first for the filename and the second with the letter "w" for opening a file for writing. then, to write several text lines to a file, we will utilise the writelines() function. notice the output from the example below:
main.py# create a new text file with multi lines code with open('readme.txt', 'w') as f: line1 = "Hi Tuts-Station.com! \n" line2 = "This is body \n" line3 = "Thank you" f.writelines([line1, line2, line3]) print("New text file created successfully!")Output
It will create readme.txt file with following text.
Hi Tuts-Station.com! This is body Thank you
Example 4:Python Create Multiline Text File with List
We shall employ the open() function in this example. It requires two parameters, the first for the filename and the second with the letter "w" for opening a file for writing. Then, using a Python list on a file, we will write many text lines using the writelines() function. notice the output sample below:
main.pymyLines = ["Hi Tuts-Station.com!", "This is body", "Thank you"] # create a new text file with multi lines code with open('readme.txt', 'w') as f: for line in myLines: f.write(line) f.write('\n') print("New text file created successfully!")Output
It will create readme.txt file with following text.
Hi Tuts-Station.com! This is body Thank you
It will help you....
Happy Pythonic Coding!