How to Create Text File in Python?
Hi,
This tutorial will provide an example of how to create text file in python. I explained simply how to make text file in python. In this article, we will implement a python create text file example. you can see python create a new text file. you will do the following things for python make new text file example.
There are a few ways to create a new text file in python. we will use open() function and write() function to create text file. I will give you some examples to create a new 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 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 2: 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 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Add Multiple Elements to a List in Python?
- Python Split String into List of Characters Example
- How to Get First Element of List in Python?
- How to Remove First Element from List in Python?
- Python Delete Files with Specific Extension Example
- Python Delete Directory if Exists Example
- Python Post Request with pem File Example
- Python Post Request with Basic Authentication Example
- Python Copy File From one Directory to Another Example
- Python Get First Date of Next Month Example
- Python Create Zip Archive from Directory Example
- How to Check if Today is Sunday or not in Python?
- Python Subtract Minutes from DateTime Example