Implement Touch Using Python

Implement touch using Python?

Looks like this is new as of Python 3.4 - pathlib.

from pathlib import Path

Path('path/to/file.txt').touch()

This will create a file.txt at the path.

--

Path.touch(mode=0o777, exist_ok=True)

Create a file at this given path. If mode is given, it is combined with the process’ umask value to determine the file mode and access flags. If the file already exists, the function succeeds if exist_ok is true (and its modification time is updated to the current time), otherwise FileExistsError is raised.

How to touch a file to a certain datetime in Python?

You can also call the touch command in python

from subprocess import call
call(["touch","-a","-m","-t","201512180130.09","file.txt"])

Using `touch` in a Windows Python File

Just don't use shell commands. Touch can be done with platform independent pathlib

from pathlib import Path

Path("some/path/file.txt").touch()

Simple open(path, "w") would also work but path on windows would need \ instead of /

Create empty file using python

There is no way to create a file without opening it There is os.mknod("newfile.txt") (but it requires root privileges on OSX). The system call to create a file is actually open() with the O_CREAT flag. So no matter how, you'll always open the file.

So the easiest way to simply create a file without truncating it in case it exists is this:

open(x, 'a').close()

Actually you could omit the .close() since the refcounting GC of CPython will close it immediately after the open() statement finished - but it's cleaner to do it explicitely and relying on CPython-specific behaviour is not good either.

In case you want touch's behaviour (i.e. update the mtime in case the file exists):

import os
def touch(path):
with open(path, 'a'):
os.utime(path, None)

You could extend this to also create any directories in the path that do not exist:

basedir = os.path.dirname(path)
if not os.path.exists(basedir):
os.makedirs(basedir)

Modyfying access times of all files in a directory using python

Nice use of examples! You're defining a function (touch) in the middle of that for loop, but it's never getting called. One other tip, when starting out sprinkling "print" statements around the script can really help understand what's going on. Also, os.utime takes a string as the first parameter, so there's no point in opening the file, you can skip that part.

import os
rootdir = 't3'
print("Checking "+rootdir)

for subdir, dirs, files in os.walk(rootdir):
for file in files:
fname = os.path.join(subdir, file)
print("touching "+fname);
os.utime(fname, None)

It's not a bad idea to isolate the logic for doing the touch into a function (maybe you need to do something more complex in the future?). That would look like:

import os

def touch(file):
print("touching "+file);
os.utime(file, None)

rootdir = 't3'
print("Checking "+rootdir)

for subdir, dirs, files in os.walk(rootdir):
for file in files:
fname = os.path.join(subdir, file)
touch(fname)

touch a directory in python (Linux)

os.utime() will allow you to set the atime and mtime of an existing filesystem object, defaulting to the current date and time.

os.utime(path)


Related Topics



Leave a reply



Submit