8

How to have Python look and see if there is a file it needs and if there isn't create one?

Basically I want Python to look for my file name KEEP-IMPORTANT.txt, but when I create my app using py2app it doesn't work because it doesn't have the file. When I try and make the file it won't work (I think because python has to generate it).

I want Python to check if the file exists so that if it does then it doesn't generate the file, otherwise it does.

user2093174
  • 93
  • 1
  • 1
  • 4
  • What do you want to do with the file? Overwrite it? Read it? Append to it? Both? – Tim Pietzcker Feb 20 '13 at 22:12
  • 2
    "When I try and make the file it won't work (I think because python has to generate it)." Hardly so. You should add more details and show the relevant code you have. – Lev Levitsky Feb 20 '13 at 22:12
  • The file needs to exist that is all – user2093174 Feb 20 '13 at 23:18
  • The application likely cannot find KEEP-IMPORTANT.txt because the current working directory for the application is different from what you expect. The default working directory is .app/Contents/Resources. If the file is a read-only data file you could copy it into the application bundle (py2app has an option for that) – Ronald Oussoren Feb 22 '13 at 13:38

4 Answers4

12

This one-liner will check to see if the file exists, and create it if it doesn't.

open("KEEP-IMPORTANT.txt", "a")
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
9

Similar question

This is the best way:

try:
    with open(filename) as file:
        # do whatever
except IOError:
    # generate the file

There's also os.path.exists(), but this can be a security concern.

Community
  • 1
  • 1
Alex Hammel
  • 345
  • 1
  • 8
  • I contrived an example to test this method for flaws. In python 2.7 permission errors raise IOError. I revoked read access for a test file but allowed write. Trying to read the file produced an IOError and I was able to overwrite it using this exception. This will not work in python 3 as reading the file produces PermissionError. This is probably a niche scenario that is unlikely to happen. I wonder what other ways IOError could occur with an existing file. – Octipi Feb 21 '13 at 01:07
  • If you don't have rw permissions for the file, won't trying to clobber it just raise another IOError? – Alex Hammel Feb 21 '13 at 17:09
  • In python 3 it would raise PermissionError. I tested the unlikely scenario that you had write permissions but not read permissions. In this case, for python 2.7, reading the file raised IOError but I could still write to it with write permissions. – Octipi Feb 21 '13 at 23:01
  • This answer introduces a race condition -- it's possible for the file to have been created by another process between the time you try to open it and the time you try to create it. The right answer is to open the file with the right options to control write, append, truncate etc. – Chris Johnson Mar 15 '16 at 17:27
1
import os.path
from os import path
    if path.exists('highScore.txt'):
         print("It exists")
    else:
         print("It doesn't exist")

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
T Schwartz
  • 11
  • 1
1

Another clean and small solution if you want to check and create the file only with out writing anything right away would be this.

import os
if os.path.exists(file_path) == False:
    open(file_path, "w").close
Nick
  • 242
  • 2
  • 12