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()
2
  • have you even tried to do anything yourself? Commented Sep 27, 2010 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. Commented Sep 27, 2010 at 16:58

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

6 Comments

Downvote for abusing conditional imports.
I have no idea why this gets downvoted. What else would you do?
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 thanks, something to think about....its not in "C:\," but on drive somewhere.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.