我有以下代碼:蟒蛇os.path.realpath不能正常工作
os.chdir(os.path.dirname(os.path.realpath(__file__)) + "/../test")
path.append(os.getcwd())
os.chdir(os.path.dirname(os.path.realpath(__file__)))
哪些應該增加/../test
到Python路徑,它這樣做,它全部採用PyDev的平穩運行之後的月食。
但是,當從控制檯第二os.chdir我午餐相同的應用程序做錯了事,實際上是錯誤的事情是os.path.realpath(__file__)
CUS它的../originalFolder/myFile.py
代替返回../test/myFile.py
。當然,我可以通過使用固定的os.chdir("../originalFolder")
來解決這個問題,但這對我來說似乎有點不對勁,但這對於日食和控制檯都有效。
P.S.我使用os.getcwd()
實際上是因爲我想確保沒有這樣的文件夾已經添加,否則我不必切換目錄的
那麼,我的方法有什麼問題,或者我已經搞砸了嗎? ?或者是什麼? :)
在此先感謝! :)
'realpath()'使路徑絕對 – 2013-02-18 07:20:24
[Here](http://helpful.knobs-dials.com/index.php/Python_usage_notes/Filesystem_stuff)是哪個命令做了什麼的一個很好的總結。 – 2013-02-18 07:27:05
雖然這不是這個topi最流行的問題,但這個答案是最正確的。謝謝 ! – egelev 2015-06-25 11:40:47