文件是否存在于Python中?

时间:2010-09-27 15:08:11

标签: python file-io path exists

  

可能重复:
  Pythonic way to check if a file exists?

如何使用python 2.6检查文件是否存在?

如果文件存在,请运行exec redo.py. 如果文件不存在exec文件start.py

该文件为0kb,但名称为Xxx100926.csv

Ans似乎是

 from os path import exists
 from __future__ import with_statement

    if exists('Xxx100926.csv'):
      from redo import main
      #execfile(u'C:\redo.py')
    else:
      from start import main
      #execfile(u'C:\start.py') 
      with open(Xxx100926.csv, 'a'): pass

 #and run main function
 main()

1 个答案:

答案 0 :(得分:3)

您可以将主要功能放在redo.pystart.py中,然后

from os path import exists

if exists('Xxx100926.csv'):
  from redo import main
else:
  from start import main

#and run main function
main()