All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Create A Text File Python

Last Updated : Mar 11, 2024

Create A Text File Python

In this article we will show you the solution of create a text file python, the open() function is used to generate a brand-new text file. There are numerous parameters in the open() function.

In the same directory as the script file, this script creates a file called tc.txt Before generating a file in the directory docs/tc.text, for example, you must make sure that the docs directory already exists.

In this case, Python generates an exception due to the absence of the manuals directory.

It was unable to produce the tc.txt file in that directory as a result. Create the docs directory first, and then create the tc.txt file in that location to resolve the problem.

Importing outside libraries is not necessary in Python in order to read and write files.

A built-in function for generating, writing, and reading files is available in Python. We will now discuss the idea of how to create a text file in python with an example.

Step By Step Guide On Create A Text File Python :-

from pathlib import Path
dir_path = Path('C:\Tc')
file_name = 'tc.txt'
file_path = dir_path.joinpath(file_name)
# check if directory exists
if dir_path.is_dir():
    # check if file already exists
    if file_path.is_file():
        print('File already exists.')
        with open (dir_path.joinpath(file_name),'w') as f:
            f.write("TalkersCode Python Code")
            print('File was created.')
    print('directory doesn't exist, please create a directory first')
  1. The 'Path' module is imported: The 'Path' module is a component of the 'pathlib' package, which is used to manipulate and represent paths (directories, files).
  2. Names for the directory and files: The string 'dir_path' provides the directory path in which we wish to create the file. The string "file_name" in the variable 'file_name' is the name of the file that we wish to create.
  3. Making the file path: The 'joinpath' method of the 'Path' module is used to make the 'file_path' variable. The file path is produced using this method by combining the directory path and the file name.
  4. Checking the directory's existence The 'Path' module's 'is_dir' method is used to determine whether the directory is present. If so, we can create the file right now. We must first create the directory if it doesn't already exist.
  5. Verifying a file's existence: To determine whether a file already exists or not, use the 'is_file' method of the 'Path' module. We won't need to create it again if it does.
  6. If the directory is present but the file is absent, we use the 'open' function to create a new file in write mode ('w'), and then we use the file object's 'write' method to enter some text. After that, we print a message confirming the creation of the file.
  7. If the directory doesn't exist, printing will fail: We print an error message stating that the directory needs to be established first if it doesn't already exist.

Conclusion :-

As a result, we have successfully learned how to create a text file in python with an example.

These are some important ideas to remember after learning about writing to files in Python.

For inserting a single string into a text file, we used the write() function, and for inserting many strings, we used the write() method.

I hope this article on create a text file python helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪