यदि आप किसी निर्देशिका से एक जमे हुए पायथन स्क्रिप्ट (py2exe का उपयोग करके जमे हुए) चला रहे हैं और स्क्रिप्ट मौजूद है, तो अलग से ड्राइव ड्राइव, पथ का निर्धारण करने का सबसे अच्छा तरीका क्या है निष्पादन स्क्रिप्ट?निष्पादन जमे हुए स्क्रिप्ट का पथ कैसे प्राप्त करें
कुछ समाधान मैं कोशिश की है
inspect.getfile(inspect.currentframe())
समस्या: पूरा पथ वापस नहीं करता है। यह केवल स्क्रिप्ट नाम देता है।
os.path.abspath(__file__)
समस्या: विंडोज
परos.path.dirname(sys.argv[0])
समस्या काम नहीं करता: खाली स्ट्रिंग देता है।
os.path.abspath(inspect.getsourcefile(way3))
अगर ड्राइव pwd से अलग है काम नहीं करेंगे
os.path.dirname(os.path.realpath(sys.argv[0]))
अगर ड्राइव pwd
यहाँ से अलग है
काम नहीं करेंगे एक न्यूनतम नहीं है - उदाहरण उदाहरण
D:\>path
PATH=c:\Python27\;c:\Users\abhibhat\Desktop\ToBeRemoved\spam\dist\;c:\gnuwin32\bin
D:\>cat c:\Users\abhibhat\Desktop\ToBeRemoved\spam\eggs.py
import os, inspect, sys
def way1():
return os.path.dirname(sys.argv[0])
def way2():
return inspect.getfile(inspect.currentframe())
def way3():
return os.path.dirname(os.path.realpath(sys.argv[0]))
def way4():
try:
return os.path.abspath(__file__)
except NameError:
return "Not Found"
def way5():
return os.path.abspath(inspect.getsourcefile(way3))
if __name__ == '__main__':
print "Path to this script is",way1()
print "Path to this script is",way2()
print "Path to this script is",way3()
print "Path to this script is",way4()
print "Path to this script is",way5()
D:\>eggs
Path to this script is
Path to this script is eggs.py
Path to this script is D:\
Path to this script is Not Found
संबंधित प्रश्न:
- How to know the path of the running script in Python?
- How do I get the path and name of the file that is currently executing?
- python, path of script [closed]
नोट
@ Fenikso समाधान करता है, तो स्क्रिप्ट पर रहता है काम करेंगे एक ही ड्राइव तुम कहाँ क्रियान्वित कर रहे हैं, लेकिन जब अपनी एक अलग ड्राइव पर, यह काम नहीं करेगा
हां, यह py2exe के साथ भी काम करता है। –
@ फेनिक्सो: यह पूरी तरह से काम करता है। इस प्रश्न को पोस्ट करने से पहले मैंने एसओ में एक ही समस्या के कुछ संदर्भ देखे हैं, लेकिन कोई जवाब सही नहीं है '। – Abhijit