• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Touch File Python – How to Touch a File Using Python

Touch File Python – How to Touch a File Using Python

May 8, 2022 Leave a Comment

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.  pandas head – Return First n Rows from DataFrame
  • 2.  Find First Occurrence in String of Character or Substring in Python
  • 3.  Get Week Number from Date in Python
  • 4.  Using Python to Calculate Average of List of Numbers
  • 5.  Get First Character of String in Python
  • 6.  Drop First Row of pandas DataFrame
  • 7.  Get Days of timedelta Object in Python
  • 8.  Using Python to Read File Word by Word
  • 9.  Length of Set Python – Get Set Length with Python len() Function
  • 10.  Truncate String in Python with Slicing

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.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy