ItSolutionStuff.com

Python Create Text File in Specific Directory Example

By Hardik Savani • October 30, 2023
Python

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...

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

Python Create an Empty Text File Example

Read Now →

How to Create Text File in Python?

Read Now →

Python Convert List into String with Commas Example

Read Now →

Python Split String into List of Characters Example

Read Now →

How to Get Unique Elements from List in Python?

Read Now →

Python Delete Files Matching Pattern Example

Read Now →

Python Delete Folder and Files Recursively Example

Read Now →

Python Post Request with Basic Authentication Example

Read Now →

Python Create Zip Archive from Directory Example

Read Now →

Python List All Dates Between Two Dates Example

Read Now →

How to Check if Today is Wednesday or not in Python?

Read Now →

How to Add Minutes to DateTime in Python?

Read Now →