ItSolutionStuff.com

Python Create an Empty Text File Example

By Hardik Savani • October 30, 2023
Python

Hi Dev,

Now, let's see an example of python create empty text file. This article will give you a simple example of python make empty txt file. This post will give you a simple example of python create empty text file in directory. I explained simply about python empty a text file. So, let us see in detail an example.

There are a few ways to create a new empty text file in python. we will use open() function and close() function to create an empty text file. I will give you some examples to create a new empty text file in python. so let's see one by one examples.

You can use these examples with python3 (Python 3) version.

Example 1: Python Empty Create Text File

In this example, we will use open() function. it takes two arguments, first argument for filename and second argument with 'w' for open a file for writing. then we simple close file using close() function. see the below example with output:

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

In this example, we will use open() function. it takes two arguments, first argument for filename and second argument with 'w' for open a file for writing. then we will use write() function to write text on file. see the below example with output:

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

In this example, we will use open() function. it takes two arguments, first argument for filename and second argument with 'w' for open a file for writing. then we will use writelines() function to write multi text lines on file. see the below example with output:

main.py

# create a new text file with multi lines code
with open('readme.txt', 'w') as f:
    line1 = "Hi ItSolutionstuff.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 ItSolutionstuff.com!
This is body
Thank you

Example 4: Python Create Multiline Text File with List

In this example, we will use open() function. it takes two arguments, first argument for filename and second argument with 'w' for open a file for writing. then we will use writelines() function to write multi text lines using python list on file. see below example with output:

main.py

myLines = ["Hi ItSolutionstuff.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 ItSolutionstuff.com!
This is body
Thank you

I hope it can help you...

Tags: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Remove null Values from the List in Python?

Read Now →

Python Get Second Last Element of List Example

Read Now →

How to Remove Last Element from List in Python?

Read Now →

Python Delete Files with Specific Extension Example

Read Now →

Python Delete File if Exists Example

Read Now →

Python Post Request with Basic Authentication Example

Read Now →

Python Copy File From one Directory to Another Example

Read Now →

Python Get File Extension from Filename Example

Read Now →

Python Create Zip Archive from Directory Example

Read Now →

Python Check if Date is Weekend or Weekday Example

Read Now →

How to Add Seconds to DateTime in Python?

Read Now →

Python PATCH Request with Parameters Example

Read Now →

Python GET Request with Parameters Example

Read Now →