Opening/Attempting to Read a file [duplicate]

2019-04-29 10:15发布

This question already has an answer here:

I tried to simply read and store the contents of a text file into an array, but:

ins = open( "file.txt", "r" )
array = []
for line in ins:
    array.append( line )
ins.close()

It gives me an error for "open":

Unresolved reference 'open' less... (Ctrl+F1) 

This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.

Can anyone explain what I'm doing wrong? Thanks

3条回答
仙女界的扛把子
2楼-- · 2019-04-29 10:53

Have you checked your preferences to make sure that you are using the correct python interpreter? ie define which version of python pyCharm should be using? This is set in your preferences:

Ctl+Shift+a (cmd+shift+a on Mac) and type "project interpreter", then set this to be python 2.x or 3.x depending on what you have installed(or whatever virtualEnv you have defined).

Then you can invalidate the cache and restart.

查看更多
Explosion°爆炸
3楼-- · 2019-04-29 11:06
array = []

with open('/path/to/file', 'r') as fp:
    for line in fp.readlines():
        array.append(line)
查看更多
Ridiculous、
4楼-- · 2019-04-29 11:17

This is a known issue in PyCharm, when it fails to update its cache of the interpreter. It happens most often if you install a new interpreter, update packages, etc.

You can search for this and related issues at the pycharm bug tracker

Its a temporary problem and will resolve itself. Keep an eye on the bottom right of the PyCharm window where it displays notifications (next to the icon of the guy in the hat). Click on this and the Event Log will have some messages for you.

If its really bothering you, you can hit ALT+ENTER and click "Ignore unresolved reference open"

查看更多
登录 后发表回答