How to Create Text File in Specific Directory in Python?

Published On: 21/12/2022 | Category: Python


Hi Guys,

This tutorial is focused on python create text file in specific directory. I’m going to show you about python create text file in specific folder. you will learn python create text file in specific directory. we will help you to give example of python create txt file in directory.

I will provide you with a straightforward example to achieve this if you need to store your newly created txt file in a particular directory. Using the open() and write() functions, we will create a text file in this example. The "readme.txt" text file will then be kept in the "files" folder. Let's look at the code example below without further ado.

So let's see bellow example:

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
New text file content line!

It will help you....

Happy Pythonic Coding!