- Touch File Python – How to Touch a File Using Python
- Updating the Timestamp of a File by Touching the File in Python
- Using the touch Module to Touch Files in Python
- Other Articles You’ll Also Like:
- About The Programming Expert
- Python Touch File | How To Implement Touch Files
- What is Path.touch()?
- Installation of pathlib
- What are the syntax, and parameters of touch file in Python?
- Syntax
- Parameter
- Using path.touch to create a file in the same directory
- Using path.touch to create a file in another directory
- What is a touch module?
- Installation of the touch module
- Using touch module to create a file in a same directory
- Using touch module to create a file in another directory
- Using touch module to create a multiple files
- Create a file if not exists
- FAQs Related to Touch File in Python
- Final Words
- Implement a Touch File in Python
- Use the pathlib.Path.touch() Function to Implement a Touch File in Python
- Use the os.utime() Function to Implement a Touch File in Python
- Use the Touch Module to Implement a Touch File in Python
- Related Article — Python File
Touch File Python – How to Touch a File Using Python
To touch a file using Python, the easiest way is with the Path.touch() function from the pathlib module. Touching a file means creating a new file or updating a timestamp of an existing file.
from pathlib import Path Path("file_name.py").touch()
When working with files and directories in Python, the ability to easily add, modify or remove files is very valuable. ‘
One such operation is touching a file. Touching a file can update the timestamp of the file in a directory or create a new file.
With the Python pathlib module, we can perform many operations to access files and directories in our environments.
The Path class of the pathlib module has a function called touch(). With Path.touch() you can touch files in your Python code.
Below is an example of how to touch a file in your working directory in Python.
from pathlib import Path Path("file_name.py").touch()
You can touch a file by passing any valid path to Path.
from pathlib import Path Path("C:/Users/TheProgrammingExpert/Documents/file_name.py").touch()
Updating the Timestamp of a File by Touching the File in Python
Touching an existing file will update the timestamp of that file. Touching an existing file will not modify any of the contents of that file.
To update the timestamp of an existing file, just pass the path of that file to Path and use the touch() function.
from pathlib import Path with open('new_file.txt', 'w') as f: f.write('This is a file with some content.') #Other steps taking time. #Update the timestamp to now Path("new_file.txt").touch()
Using the touch Module to Touch Files in Python
You can also use the Python touch module to touch files in Python.
The touch() function in the touch module allows us to touch one or more files in a single call.
Below is an example of how to use the touch module touch() function in Python.
import touch touch.touch("file_name.py") touch.touch(["file_name1.py", "file_name2.py"])
Hopefully this article has been useful for you to learn how to touch files in Python.
Other Articles You’ll Also Like:
- 1. Get All Substrings of a String in Python
- 2. Python turtle dot() – Draw Dot on Turtle Screen
- 3. Check if List is Subset of Another List in Python
- 4. Remove Empty Lists from List in Python
- 5. Check if Variable is Tuple in Python
- 6. Examples of Recursion in Python
- 7. Get Days of timedelta Object in Python
- 8. Using Python to Flatten Array of Arrays
- 9. Python Get Yesterday’s Date
- 10. How to Write Pickle File to AWS S3 Bucket Using Python
About The Programming Expert
The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.
Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.
At the end of the day, we want to be able to just push a button and let the code do it’s magic.
You can read more about us on our about page.
Python Touch File | How To Implement Touch Files
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:
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”.
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”.
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:
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”.
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”.
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”.
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!
Implement a Touch File in Python
- Use the pathlib.Path.touch() Function to Implement a Touch File in Python
- Use the os.utime() Function to Implement a Touch File in Python
- Use the Touch Module to Implement a Touch File in Python
Unix systems have a utility command called touch . This utility sets the access and modification times of the given file to the current time.
We will discuss how to implement the touch file in Python.
Use the pathlib.Path.touch() Function to Implement a Touch File in Python
The pathlib module allows us to create Path objects to represent different filesystem paths and work between operating systems.
We can use the pathlib.Path.touch() function to emulate the touch command. It creates a file in Python at the specified path. We specify the file mode and access flags using the mode parameter.
It also accepts an exist_ok parameter which is True by default. If this is set to False, an error is raised if the file already exists at the given path.
from pathlib import Path Path('somefile.txt').touch()
Use the os.utime() Function to Implement a Touch File in Python
The os.utime() function sets the access and modification time. We can specify the time for both using the times parameter. By default, both the values are set to the current time.
We will create a function to open a file using the open() function and then use the os.time() function. The file will be opened in append mode.
import os def touch_python(f_name, times=None): with open(f_name, 'a'): os.utime(f_name, times) touch_python('file.txt')
Use the Touch Module to Implement a Touch File in Python
The touch module is a third-party module that can emulate the Unix touch command. We can use it to create a touch file in Python. We will use the touch.touch() function with a specified filename and path.
import touch touch.touch('somefile.txt')
The advantage of this method over the rest is that we can also use it to create multiple files. For this, we will pass the filename and their paths as elements of a list.
See the following example.
import touch touch.touch(['somefile.txt','somefile2.txt'])
Any file which already exists will be replaced.
Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.