views:

126

answers:

1

Possible Duplicate:
Pythonic way to check if a file exists?

How can Check if file exist with python 2.6?

If file exists run exec redo.py. If file does not exists exec file start.py

The file is a 0kb, but name Xxx100926.csv

Ans seems to be

 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()
A: 

you can put main function in redo.py and start.py and then

from os path import exists

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

#and run main function
main()
jcubic
Downvote for abusing conditional imports.
ddaa
I have no idea why this gets downvoted. What else would you do?
THC4k
How is this an "abuse"
Falmarri
I guess "with open(thepath, 'a'): pass" would go under else and I too dont understand logic behind downvote, But hope to learn why? I was thinking along these lines but was thinking using execfile(u'C:\start.py') and execfile(u'C:\redo.py') under if/try statements
I would suggest against execFile because that's a pretty big security risk if someone were to replace your start and redo files with something malicious. Especially on windows in the C:\ directory. At least with jcubic's answer it has to be a correct python package. And I think it would be easier for validation checks this way
Falmarri
@Falmarri thanks, something to think about....its not in "C:\," but on drive somewhere.
Well it's still a security issue, although depending on your application it may or may not be something to think about
Falmarri