想要用 Python 的 pygeoip 库通过 ip 获取经纬,然后给百度地图,在页面显示,结果在形成对象这儿卡住了 各位大佬,帮吗看一下是啥情况?

2018-07-26 19:13:11 +08:00
 Yycreater
import pygeoip
gi = pygeoip.GeoIP('GeoLiteCity.dat')
response = gi.record_by_addr(ip)
我正常导入库,路径也正确,想要用 Python 的 pygeoip 库通过 ip 获取经纬,然后给百度地图,在页面显示,结果在形成对象这儿卡住了
各位大佬,帮吗看一下是啥情况?

分割线

File "/usr/local/lib/python2.7/site-packages/pygeoip/__init__.py", line 118, in __init__
self._fp = codecs.open(filename, 'rb', ENCODING)
File "/usr/local/lib/python2.7/codecs.py", line 896, in open
------------if mode[:1] not in set('rwa'):
--------------mode = 'r' + mode
Open an interactive python shell in this frame if 'b' not in mode:
------------# Force opening of the file in binary mode
------------mode = mode + 'b'
----file = __builtin__.open(filename, mode, buffering) # 报这个错误!!!
----if encoding is None:
--------return file
----info = lookup(encoding)
----srw = StreamReaderWriter(file, info.streamreader, info.streamwriter, errors)
----# Add attributes to simplify introspection
IOError: [Errno 2] No such file or directory: 'GeoLiteCity.dat'
2686 次点击
所在节点    Python
6 条回复
xiexingjia
2018-07-26 19:20:09 +08:00
No such file or directory: 'GeoLiteCity.dat'
Yycreater
2018-07-26 19:38:06 +08:00
文件我直接放在同级目录下了,文件路径没问题,刚刚在项目外面试了一下{'city': u'Shenzhen', 'region_code': u'30', 'area_code': 0, 'time_zone': 'Asia/Chongqing', 'dma_code': 0, 'metro_code': None, 'country_code3': 'CHN', 'latitude': 22.533299999999997, 'postal_code': None, 'longitude': 114.13330000000002, 'country_code': 'CN', 'country_name': 'China', 'continent': 'AS'}
很 ok !项目内就是不行。。。。
KeatingSmith
2018-07-26 19:42:51 +08:00
使用绝对路径吧。

有可能你的 Python 运行路径与文件路径不一致,而你又使用的是相对路径
Yycreater
2018-07-26 19:52:52 +08:00
试了,还是报那个错误~~~
lieh222
2018-07-27 08:49:20 +08:00
print(os.getcwd())
self._fp = codecs.open(filename, 'rb', ENCODING)
您瞧瞧打印的当前路径对不对
Yycreater
2018-07-27 09:48:56 +08:00
@lieh222 托您的福,用 print os.path.realpath(__file__)
print '*' * 50 方法调好了路径

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/474450

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX