[py]python __file__ 与argv[0]
2021-07-20 19:08
标签:href sys.argv ref pat bin path 相对 print code http://andylin02.iteye.com/blog/933237 获取当前文件的全路径 import os print(file) import sys print(sys.argv[0]) print(os.path.abspath(sys.argv[0])) dirname, filename = os.path.split(os.path.abspath(sys.argv[0])) ··· [py]python __file__ 与argv[0] 标签:href sys.argv ref pat bin path 相对 print code 原文地址:https://www.cnblogs.com/iiiiiher/p/9516974.htmlimport os,sys
dirname, filename = os.path.split(os.path.abspath(sys.argv[0]))
print "running from", dirname
print "file is", filename
__file__ 是用来获得模块所在的路径的,这可能得到的是一个相对路径,比如在脚本test.py中写入:
#!/usr/bin/env python
print __file__
获取全路径路径+文件名
···!/usr/bin/env python
coding=utf-8
E:/learn/djangotest/mysite/demo/01.py
E:/learn/djangotest/mysite/demo/01.py
E:\learn\djangotest\mysite\demo\01.py
print(dirname)E:\learn\djangotest\mysite\demo
上一篇:python
文章标题:[py]python __file__ 与argv[0]
文章链接:http://soscw.com/essay/106713.html