python 操作oracle 执行脚本_python、abaqus执行脚本路径
python中獲取執行腳本路徑方法
1、sys.path[0]:獲取執行腳本目錄絕對路徑
#每次執行腳本時,python會將執行腳本目錄加入PYTHONPATH環境變量中(sys.path獲取) #!/usr/bin/python3 import os import sys print(sys.path) print(sys.path[0]) 執行結果: [root@localhost tmp]# ./py_test1/pytest24.py ['/tmp/py_test1', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6', '/usr/local/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages', '/usr/local/lib/python3.6/site-packages/pip-9.0.1-py3.6.egg'] /tmp/py_test12、sys.argv[0]:獲取腳本執行本身路徑;
#!/usr/bin/python3 import os import sys print(sys.argv[0]) 執行1結果: [root@localhost tmp]# ./py_test1/pytest24.py #相對路徑執行腳本則會返回相對路徑 ./py_test1/pytest24.py 執行2結果: [root@localhost tmp]# /tmp/py_test1/pytest24.py #絕對路徑執行腳本則返回絕對路徑 /tmp/py_test1/pytest24.py注:sys.argv[0]獲取得不是腳本目錄路徑,而是腳本本身執行時的路徑!
3、__file__:同sys.argv[0]相似,獲取腳本執行本身路徑:
#!/usr/bin/python3 import os import sys print("sys.argv[0] Output:",sys.argv[0]) print("__file Output:",__file__) 執行1結果: [root@localhost tmp]# ./py_test1/pytest24.py #相對路徑執行腳本則會返回相對路徑 sys.argv[0] Output: ./py_test1/pytest24.py __file Output: ./py_test1/pytest24.p執行2結果: [root@localhost tmp]# /tmp/py_test1/pytest24.py #絕對路徑執行腳本則會返回絕對路徑 sys.argv[0] Output: /tmp/py_test1/pytest24.py __file Output: /tmp/py_test1/pytest24.py注:__file__獲取得不是腳本目錄路徑,而是腳本本身執行時的路徑!
4、os.path.abspath(__file__)和os.path.realpath(__file__):獲取腳本執行本身的絕對路徑
通過獲取__file__路徑,然后轉換成絕對路徑
#!/usr/bin/python3import os import sysprint("__file Output:",__file__) print(os.path.abspath(__file__)) print(os.path.realpath(__file__)) 執行結果: [root@localhost tmp]# ./py_test1/pytest24.py __file Output: ./py_test1/pytest24.py /tmp/py_test1/pytest24.py /tmp/py_test1/pytest24.py注:os.path.abspath(__file__)和os.path.realpath(__file__)獲取得是腳本本身的絕對路徑!
------------------------------------------------------
ABAQUS與執行腳本路徑
ABAQUS cae script=xxx.py 命令下
1、sys.path[0],;
2、sys.argv[0],cae執行文件路徑;
3、sys.argv[5],工作路徑;
4、os.getcwd(),同3工作路徑;
5、os.path.abspath(__file__),出錯;
難以獲取py文件路徑
ABAQUS cae script=xxx.py 命令下
1、sys.argv[3],py文件路徑;
待補充
總結
以上是生活随笔為你收集整理的python 操作oracle 执行脚本_python、abaqus执行脚本路径的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pythonxml解析拿到控件坐标_Py
- 下一篇: websocket python爬虫_p