How to indicate the current directory of the script not me?
This question already has an answer here:
__file__
is the path of the script, albeit often a relative path. Use:
import os.path
scriptdir = os.path.dirname(os.path.abspath(__file__))
to create an absolute path to the directory, then use os.path.join()
to open your file:
with open(os.path.join(scriptdir, './input1.txt')) as openfile:
If setuptools/distribute are installed; you could use pkg_resources functions to access files:
import pkg_resources
data = pkg_resources.resource_string(__name__, 'input1.txt')
链接地址: http://www.djcxy.com/p/54706.html
上一篇: 你如何在Python中获取当前的本地目录
下一篇: 如何指示脚本的当前目录不是我?