文章内容

2020/9/15 17:57:01,作 者: 黄兵

Python 设置当前文件路径

最近项目上需要读取第三方文件,项目结构如下:

Python获取路径代码如下:

geoip2_path = os.path.join(os.getcwd(), 'app\\geoip\\GeoLite2-City.mmdb')
    if os.path.exists(geoip2_path):
        with geoip2.database.Reader(geoip2_path) as reader:
            response = reader.city('121.62.105.35')
            get_country = response.country.names['zh-CN']
            print(response.city.name)
            print(get_country)

这里使用os.getcwd()获取工作目录字符串,之后与app\geoip\GeoLite2-City.mmdb合并路径。


参考资料:

1、python doc os.getcwd

2、python doc os.path.join


黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - Python 设置当前文件路径

分享到:

发表评论

评论列表