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()
Community
  • 1
  • 1
Merlin
  • 24,552
  • 41
  • 131
  • 206
  • have you even tried to do anything yourself? – SilentGhost Sep 27 '10 at 15:14
  • @silent I'm migrating to python, It much easier to ask simple coding questions. And, get ideas from other people than code on my own and think everything is right and the best way to do it. I have code for most of questions, but not in python. And, also its more fun to read and understand other people coding ideas. – Merlin Sep 27 '10 at 16:58

1 Answers1

3

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
  • 61,973
  • 54
  • 229
  • 402
  • 3
    Downvote for abusing conditional imports. – ddaa Sep 27 '10 at 15:41
  • 6
    I have no idea why this gets downvoted. What else would you do? – Jochen Ritzel Sep 27 '10 at 15:47
  • 1
    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 – Merlin Sep 27 '10 at 15:55
  • 1
    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 Sep 27 '10 at 15:59
  • @Falmarri thanks, something to think about....its not in "C:\," but on drive somewhere. – Merlin Sep 27 '10 at 16:05
  • Well it's still a security issue, although depending on your application it may or may not be something to think about – Falmarri Sep 27 '10 at 16:47