Pythonモジュール内蔵の変数とその効果

1. __file__

ここで、モジュール:OS

可変効果:現在のファイルへのポイント

現在のファイルのフルパス:os.path.abspath(__FILE__

現在のファイルの内容:os.path.dirname(os.path.abspath(__FILE__))

現在のファイルの親ディレクトリのディレクトリを所属:os.path.dirname(os.path.dirname(os.path.abspath(__FILE__))

 

猫filelocation.py
 輸入OSの
 印刷__FILE__ プリント(os.path.abspath(" filelocation.py " ))
 プリント(os.path.abspath(__FILE__ ))
 プリント(os.path.dirname(os.path.abspath(__FILE__ )))
 印刷(os.path.dirname(os.path.dirname(os.path.abspath(__FILE__ ))))
运行:
filelocation.py
 /ホーム/テスト/ CodeProjects / PythonProjects /テスト/ filelocation.py
 /ホーム/テスト/ CodeProjects / PythonProjects /テスト/ filelocation.py
 /ホーム/テスト/ CodeProjects / PythonProjects / テスト
/ホーム/テスト/ CodeProjects / PythonProjects

 

 

 

 

おすすめ

転載: www.cnblogs.com/DesignerA/p/11618601.html