To implement touch in Python, you can use the pathlib module’s “Path.touch()” method. It will create any file in any specified directory.
Syntax
Path.touch(mode=0o666, exist_ok=True)
Parameters
- mode: It suggests in which mode you want to create a file.
- exist_ok: If the file already exists, 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
from pathlib import Path
Example: Touch a single file
Before touching a single file:
from pathlib import Path
Path("/Users/krunal/Desktop/code/pyt/database/file.txt").touch()
After touching a single file:
That is it.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.