获取¨IndexError:列表索引超出范围¨这是一个错误吗?

时间:2017-08-12 21:59:29

标签: python-2.7

我似乎无法在Python 2.7.13代码中找到错误。当我尝试运行它时,会显示以下内容:

“IndexError:列表索引超出范围”

for d in dopant[1:]:  
  for s in xrange(1,3,2): 
    for k in xrange(0,1):

      # creates folder
      try:
        os.makedirs("path")
      except OSError:
        if not os.path.isdir("path"):
           raise

      # enters that folder
      os.chdir("path")

      file2 = open("atomicXYZ","a+")
      stdin=subprocess.PIPE, stdout = file2).stdin
      subprocess.Popen(['cat', '/path/file'], stdout = cmd1)

      file2.seek(0)

      # The following reads atomicXYZ and converts its contents to tuples
      result = []
      with file2 as fp:
        for i in fp.readlines():
          tmp = i.split()
          try:
             result.append((float(tmp[0]), float(tmp[1]), float(tmp[2])))
          except:pass
        # As a check, I access the last line of the tuple
        x,y,z = result[len(result)-1]
      os.chdir("..")

那是错误出现的时候。这是令人惊讶的,因为atomicXYZ不是空的,你可以在这里看到:

atomicXYZ

0.309595018   0.070879924   0.041045030
0.600985479   0.103996517   0.130482163
0.982347083  -0.008801119  -0.088718291
0.266923601   0.125720284  -0.038070136
0.520845390   0.163282973   0.061118496
0.812787033   0.194089924   0.131124240
0.398054509   0.270533816  -0.097226923
0.673016094   0.332428625   0.006571612
0.968473946   0.356972107   0.087712083
0.896549601   0.449435057   0.027658530
0.602586223   0.391867525  -0.070503370
0.732266134   0.576057624  -0.111890811
1.018201372   0.643127004  -0.009288985
0.914029765   0.703744085  -0.066356115

更奇怪的是,当我将整个代码分成两个代码时 - 一个用于写入原子坐标,另一个用于读取坐标 - 它可以工作。

我做错了什么?

0 个答案:

没有答案