3

I'm writing a function and I want it to touch a file so that I can write to that file. If the file doesn't exist, I will get an error. How can I say that?

Bruno
  • 119,590
  • 31
  • 270
  • 376
magnetar
  • 6,487
  • 7
  • 28
  • 40
  • 1
    The [open()](http://docs.python.org/library/functions.html#open) documentation may be interesting to look at. –  Oct 12 '10 at 19:51
  • 12
    What you have described is not the purpose of `touch` – Daenyth Oct 12 '10 at 19:52
  • 7
    `touch` will update the timestamp of an existing file, or create a new file if it doesn't exist. – Fosco Oct 12 '10 at 19:53
  • @Daenyth sorry. What's the purpose then? – magnetar Oct 12 '10 at 19:55
  • possible duplicate of [Implement touch using Python?](http://stackoverflow.com/questions/1158076/implement-touch-using-python) – SilentGhost Oct 12 '10 at 21:10
  • @SilentGhost: I think it's only a duplicate if you take this at face value. It's really an XY problem, so the dupe doesn't actually help the main issue. – Daenyth Oct 13 '10 at 15:55
  • @SilentGhost: He doesn't know how to handle IOError in this context – Daenyth Oct 13 '10 at 17:57
  • @SilentGhost You're right. I didn't. The question is more or less a duplicate, but the OP in the one you linked to does not have the same exact issue as me, so it's not an exact duplicate. He's looking at it from a more abstracted perspective, and I'm just a newbie grappling with open(file, readmethod) – magnetar Oct 14 '10 at 01:20
  • 3
    Very misleading title! As others have suggested, this is not what `touch` does. You might want to consider to rephrase this! – exhuma Jul 06 '12 at 11:42

3 Answers3

12

Just open the file for writing and it will be created if it doesn't exist (assuming you have proper permission to write to that location).

f = open('some_file_that_might_not_exist.txt', 'w')
f.write(data)

You will get an IOError if you can't open the file for writing.

awesomo
  • 8,526
  • 2
  • 21
  • 24
  • 4
    Note, this is the correct answer to the question detailed, but this is not a "Python equivalent of touch" (as the question title may imply) – Peter Gibson Feb 22 '12 at 00:47
  • For those innocently looking for a Python implementation of `touch`, note the above is not comparable to unix `touch` because ***USING THE `w` OPTION WILL DELETE THE CONTENTS OF THE FILE IF IT ALREADY EXISTS***. `touch` only changes timestamps if the file already exists. For a Python implementation of `touch`, see http://stackoverflow.com/questions/1158076/implement-touch-using-python. – Chris Johnson Mar 28 '14 at 15:09
7

Per the docs, os.utime() will function similar to touch if you give it None as the time argument, for example:

os.utime("test_file", None)

When I tested this (on Linux and later Windows), I found that test_file had to already exist. YMMV on other OS's.

Of course, this doesn't really address writing to the file. As other answers have said, you usually want open for that and try ... except for catching exceptions when the file does not exist.

GreenMatt
  • 18,244
  • 7
  • 53
  • 79
  • Here is a version that addresses the "file must exist" issue http://stackoverflow.com/questions/1158076/implement-touch-using-python – Peter Gibson Feb 22 '12 at 00:49
0

if you actually want to raise an error if the file doesn't exist, you can use

import os
if not os.access('file'):
    #raise error
f = open('file')
#etc.
Ant
  • 5,151
  • 2
  • 26
  • 43