0

I want to do something like the following but im not sure if it is allowed:

try:
  import module1
except Exception,e :
  if os.path.exists("module2.py"):
    print "err importing module1 "+str(e)
  else:
    pass

I only want to get the print statement if module1 does not exist but there is a file in the current directory named module2.py.

I have tested it where module1 cannot be imported and the file module2.py exists, but I never see the print statement. Can someone advise me what im doing wrong? Is it even possible to have conditional statements within try/except blocks?

oggiemc
  • 273
  • 2
  • 4
  • 12
  • You can use any code in try/except. Try veryfing `os.getcwd()` returns what you expect. – Tomo Nov 18 '14 at 11:38
  • Just because the file exists somewhere does not mean that it is found in `sys.path`. – cdarke Nov 18 '14 at 11:41
  • If module1 is somehow not Imported, then It will execute except block. Now there is an if condition in the except block, where you wanted to look for the existence of module2.py in the current working directory. Is my understanding of your requirement correct? Also Have you tried giving the absolute path of module2.py in the os.path.exists() – kvivek Nov 18 '14 at 12:02
  • @kvivek The code that i posted above is from a common python script that is going to be located in two directories, /dir1 and /dir2. i cannot use the absolute path because i want to check the current directory which is either going to be /dir1 or /dir2 – oggiemc Nov 18 '14 at 12:48
  • are you running the code with an IDE ? –  Nov 18 '14 at 13:52

1 Answers1

0

There is nothing wrong with your code. As @Tomo said, you can use any code within the try block.

You said:

I have run the test where module1 cannot be imported and the file module2.py exists

This means permissions are not granted to perform os.stat() on your file module2.py. So all what you have to do is to chmod your file with the needed permissions (you can chmod it in 777 as a first test).

EDIT:

As the first method does not give what you expect, apply what EAFP states:

Easier to ask for forgiveness than permission.

So, your code should run this new conception:

try:
  import module1
except Exception,e :
  print"Module 1 could not be imported"+str(e)
try:
  md2=os.path.exists("module2.py"):
  print md2
except Exception,c:
    pass
  • I have tried changing the permissions (chmod 777). The print statement is still not executed. – oggiemc Nov 18 '14 at 12:51
  • ok, i tried that and "print md2" is printing False. This is strange because module2.py definintely exists in the current directory and if i run the following command directly from the python interpreter in the current directory i get True: >>> print os.path.exists("module2.py") True – oggiemc Nov 18 '14 at 13:30
  • @oggiemc It is strange. I am sorry, I can not help you more. That is simply strange. –  Nov 18 '14 at 13:34
  • @oggiemc are you using a given IDE to run your code ? –  Nov 18 '14 at 13:45
  • ok, i have just figured out the problem. i was using an alias to start the script that contains the problematic code : alias shell='python /apps/sm/bin/shell/shell.py'.. and i was executing this alias in a different directory to /apps/sm/bin/shell..when i navigate to /apps/sm/bin/shell and execute shell.py directly then the code md2 evaluates to True as expected. Im a little surprised though that the code does not work when i use the alias? I mean even though an alias is used to start the shell.py script, surely this should now be from the perspective of /apps/sm/shell/shell.py – oggiemc Nov 18 '14 at 14:07
  • instead of the directory that the alias was executed from? Can someone explain this to me? – oggiemc Nov 18 '14 at 14:08