Python Create Text File in Specific Directory Example
Hello Dev,
This post is focused on python create text file in specific directory. If you have a question about how to create a text file in a directory in python then I will give a simple example with a solution. This example will help you python create text file in specific folder. I am going to show you about python create text file in specific directory.
If you need to store your newly created txt file in a specific directory then I will give you a simple example to do this. In this example, we will create text file using open() and write() function. Then we will store "readme.txt" text file in "files" folder. Without any further ado, let's see below code example.
You can use these examples with python3 (Python 3) version.
Example 1:
main.py
textFilePath = "files/readme.txt" # create new text file code with open(textFilePath, 'w') as f: f.write('New text file content line!') print("New text file created successfully!")
Output:
It will create readme.txt file in "files" folder with following text.
New text file content line!
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
- Python Create an Empty Text File Example
- How to Create Text File in Python?
- Python Convert List into String with Commas Example
- Python Split String into List of Characters Example
- How to Get Unique Elements from List in Python?
- Python Delete Files Matching Pattern Example
- Python Delete Folder and Files Recursively Example
- Python Post Request with Basic Authentication Example
- Python Create Zip Archive from Directory Example
- Python List All Dates Between Two Dates Example
- How to Check if Today is Wednesday or not in Python?
- How to Add Minutes to DateTime in Python?