12

I'd like to use Python 3.3.3 to test for the existence of a backup file.

Something like this:

if backup does not exist:
    create it
    write to backup

otherwise:
    run other procedures not related to backup file

Any suggestions?

linusg
  • 6,289
  • 4
  • 28
  • 78
user1505713
  • 615
  • 5
  • 20

2 Answers2

5

Use os.path.exists or os.path.isfile:

>>> import os
>>> os.path.isfile('/path/to/file')
False
falsetru
  • 357,413
  • 63
  • 732
  • 636
2

Take a look at os.path.exists.

nmichaels
  • 49,466
  • 12
  • 107
  • 135