How to Implement Touch File in Python
The touch is a Unix utility command used to create, access, or modify files. The touch command is a conventional command used in the UNIX/Linux OS, which is used to create and edit the timestamps of a file.
Touch File in Python
Python provides a built-in module called pathlib that offers the Path.touch() function, creating a file at this provided path.
To create a file using the command line in Python, use the pathlib module’s Path.touch() function. If the mode argument is provided, then it is combined with the process’ umask value to define the file mode and access flags.
Syntax
Path.touch(mode=0o666, exist_ok=True)
Parameters
It takes a mode parameter that suggests in which mode you want to create a file.
If the file already exists, then the function succeeds if the exist_ok argument is true (and its modification time is updated to the current time). Otherwise, FileExistsError is raised.
Import pathlib library
To use the Path.touch() function in your Python program, you need to import the pathlib module.
from pathlib import Path
If you are using Python 2.x, then you can install the module using the following command.
pip install pathlib
If you are using Python3, then the pathlib module is already installed in your machine since it is an inbuilt module.
Snippet for Python touch file
from pathlib import Path Path("path/to/file.txt").touch()
Example
Let’s touch a new file in our current working directory using the Path.touch() method.
from pathlib import Path Path("/Users/krunal/Desktop/code/pyt/database/file.txt").touch()
If you run the above code, it will create a new file called file.txt to the provided file path.
Touch module in Python
There is a third-party module called touch, which you can install and use its function to create a new file. The Touch module is used to create any file on any specified directory. The touch module is equivalent to the Linux command ‘touch‘.
To install the touch module, type the following command in the terminal.
python3 -m pip install touch
Now, import the touch module into your program to use its touch() function.
import touch touch.touch("/Users/krunal/Desktop/code/pyt/database/info.txt")
It will create a new file called info.txt file in the provided directory.
How to touch multiple files in Python
To touch multiple files in Python, use the touch module’s touch() function and pass the list of file names as or file paths as arguments, which will create multiple files in the provided paths.
import touch touch.touch(["info.txt", "data.txt", "apple.txt"])
It will create three files in your current working directory. If the files already exist, then it will replace them with new files.
Touch a file using a custom function in Python
If you don’t want to use the pathlib or touch module, then you can define your own function and write the code that will create a file in your required directory. We will use Python’s OS module.
import os def touch(file_name, times=None): if os.path.exists(file_name): os.utime(file_name, None) else: open(file_name, 'a').close() touch("info.txt")
This will eliminate the race conditions while creating new files.
That is it for the Python touch file tutorial.