Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File tests in Python? [duplicate]

Tags:

python

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?

like image 654
user1505713 Avatar asked Jan 24 '14 16:01

user1505713


2 Answers

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

>>> import os
>>> os.path.isfile('/path/to/file')
False
like image 196
falsetru Avatar answered Oct 26 '22 22:10

falsetru


Take a look at os.path.exists.

like image 28
nmichaels Avatar answered Oct 26 '22 22:10

nmichaels