Monday, 15 March 2010

php - How can I find the currently executing script file and path in python? -


I am currently trying to get the file name and directory of the script executed and this is the code that works :

  import inspection, OS print os.path.dirname (os.path.abspath (inspect.getfile (inspect.currentframe ()))   

In php, it's just echo dynamism (__file__) .

It is suggested that dirname (__file____) should work in python, but it runs an empty string while running through the command line.

My question is why do I have to type all this code to get the file name:

  print os.path.dirname (os.path.abspath (inspect .getfile (inspect.currentframe ())))   

And am I doing it wrong?

__ file __ value for the current script that was passed to Python The exact same path is; Python yourcript.py is thus a relative path and does not include the directory name. You need to make this a complete path, but you need to use not to inspection :

  import os.path Print os. Path.dirname (os.path.abspath (__ file__))   

demo:

  $ cat /private/tmp/test.py import os. Path print os.path.dirname (os.path.abspath (__ file__)) $ python /private/tmp/test.py/private/tmp $ cd / private / tmp / $ python test.py / private / tmp   

In PHP, you have the luxury of a web server that executes your file with a full path specified entirely, so you have never seen this 'problem' crop.

No comments:

Post a Comment