Python Create Text File in Specific Directory Example

By Hardik Savani October 30, 2023 Category : 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:

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!")


It will create readme.txt file in "files" folder with following text.

New text file content line!

I hope it can help you...

Tags :