In this article, we are going to learn about python touch files. With that, we will learn how to implement touch files in python. Simply, we can say we will learn how to create a new file in a particular path with a particular extension. To implement this, we are using some modules. We will learn about all of this in a detailed manner.
To create a new file in a needed directory, we can either use path.touch() function or touch module. Both of them play a similar role. The only difference is that it is implementing using the pathlib module. We can’t create multiple files at a time. And using the touch module, we can create multiple files at a time.
What is Path.touch()?
Path.touch is a function that is already available in the pathlib module. This path.touch() function is useful to create a file in a current working directory or some other specified directory. But we can’t create multiple files at a time.
Installation of pathlib
If you are using python 3.x, then no need to install the pathlib module. It is inbuilt in python. If you are using python 2.x then you have to install the pathlib module by using the command line:
pip install pathlib
What are the syntax, and parameters of touch file in Python?
Syntax
Path.touch(mode=0o666, exist_ok=True)
Parameter
mode: In which mode do we have to create a file.
Using path.touch to create a file in the same directory
from pathlib import Path
Path("file.py").touch()
print("File is created in a current working directory")
Using path.touch() function to create a python file in the same directory. I am following the syntax. So if the file is created successfully, it will display the statement like “File is created in a current working directory”.
Output
File is created in current working directory
Now we can check in python programs whether the file is there or not.
You can see that file in the same path.
Using path.touch to create a file in another directory
from pathlib import Path
Path("E:/files/python created this file.txt").touch()
print("File is created in a specified directory")
Using path.touch() function to create a python file in some other directory. I am following the syntax. So if the file is created successfully, it will display the statement like “File is created in a specified directory”.
Output
File is created in a specified directory
Now we can check in a specified folder whether the file is there or not.
You can see that file in a specified path.
What is a touch module?
The touch module is also useful to create a new file like the pathlib module. We need to install a module library. This module is useful to create a file in a current working directory or some other specified directory. And also, we can create multiple files at a time.
Installation of the touch module
The pip command to install the touch module is:
pip install touch
Using touch module to create a file in a same directory
import touch
touch.touch("test.py")
print("File is created in a current working directory")
Using touch module to create a python file in the same directory. If the file is created successfully, it will display a statement like “File is created in a current working directory”.
Output
File is created in a current working directory
Now we can check in python programs whether the file is there or not.
You can see that file in the same file path.
Using touch module to create a file in another directory
import touch
touch.touch("E:/files/new file.txt")
print("File is created in a specified directory")
Using touch module to create a python file in a specified directory. If the file is created successfully, it will display a statement like “File is created in a specified directory”.
Output
File is created in a specified directory
Now we can check in a specified folder whether the file is there or not.
You can see that file in a specified path.
Using touch module to create a multiple files
import touch
touch.touch(["E:/files/success file1.txt", "E:/files/success file2.txt"])
print("Multiple files are created successfully")
Importing touch module. Here we are going to create multiple files at the same time. To create multiple files, we have to give the file names in a list. If the file is created successfully, it will display a statement like “Multiple files are created successfully”.
Output
Multiple files are created successfully
Now we can check in a specified folder whether the files are there or not.
You can see that files are in a specified path.
Create a file if not exists
from pathlib import Path
Path("E:/files/exist.txt").touch(exist_ok=True)
exist_ok creates a new file if the file does not exist. If the file already exists, it will do nothing to that. Simply it leaves that as it is.
Now I’m trying to create a new file. Let us see whether the new file is created or not.
A new file named exist is created.
Now we will try this code with an existing file.
from pathlib import Path
Path("E:/files/newfile.txt").touch(exist_ok=True)
The file named new file already exists in a specified directory, so it did nothing with that.
FAQs Related to Touch File in Python
We can use the pathlib module to implement the touch file in python.
We can use a path.touch() function and touch module to create new files.
Final Words
Here we came to the end of the article. We have learned about the touch file in python. And also, we learned about the path.touch(), and touch module. This article is beneficial to learn. Creating a new file is our day-to-day application. So doing that with python is awesome. Try to code on your own. Learn python with us!
Nice