我想使用Python 3.3.3來測試備份文件的存在。Python中的文件測試?
事情是這樣的:
if backup does not exist:
create it
write to backup
otherwise:
run other procedures not related to backup file
有什麼建議?
我想使用Python 3.3.3來測試備份文件的存在。Python中的文件測試?
事情是這樣的:
if backup does not exist:
create it
write to backup
otherwise:
run other procedures not related to backup file
有什麼建議?
使用os.path.exists
或os.path.isfile
:
>>> import os
>>> os.path.isfile('/path/to/file')
False
請插入這裏引入競爭條件等通常的警告... –
請先詢問,此問題已被回答。 – njzk2
重複http://stackoverflow.com/questions/82831/how-do-i-check-if-a-file-exists-using-python?lq=1 – Vader