【问题标题】:python erroneously skipping last line of last file in a listpython错误地跳过列表中最后一个文件的最后一行
【发布时间】:2013-02-19 02:00:07
【问题描述】:

我用 Python v3.3 编写了一个程序,它依次打开文件列表并使用每个文件中的数据执行操作。但是,由于某种原因,程序在打开文件时始终忽略列表中最后一个文件的最后一行。之前的所有文件都可以正常读取。这些文件本身具有相同的格式,并且列表中的最后一个文件中没有其他所有其他文件中都不存在的额外空格或换行符。

代码如下:

counter3=0
for counter3 in range(counter3,numSteps):
# open up each step in the list of steps across the chromosomal segment:
    L=shlex.shlex(stepFileIndex[counter3],posix=True)
    L.whitespace += '\t'
    L.whitespace_split = True
    L=list(L)
    #print(L)
    stepNumber = int(L[0])
    stepStart = int(L[1])
    stepStop = int(L[2])
    stepSize = int(stepStop-(stepStart-1))
#Now open the file of SNPs corresponding with the window in question and convert it into a list:
    currentStepFile = open(("C:/Users/gwilymh/Desktop/Python/Sliding Window Analyses-2/%s_%s_step_%s.txt")%(str(segmentNumber),str(segmentName),str(counter3+1)),'r')
    currentStepFile = list(currentStepFile)
    nSNPsInCurrentStepFile = len(currentStepFile)
    print("number of SNPs in this step is:", nSNPsInCurrentStepFile)
    print(currentStepFile)

列表中的最后两个文件如下:

1_segment1_step_7.txt
['1503', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'C']
['1505', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'G']

1_segment1_step_8.txt
['1950', 'G', 'G', 'G', 'C', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G']
['1967', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'G']

【问题讨论】:

  • numSteps 的对手在哪里?
  • 那里不应该有currentStepFile.readlines() 吗?我只在文件句柄上看到一个列表...
  • 不,技术上可行。
  • @Fredrik no, list(filehandle) 会导致整个文件的隐式迭代/读取,就像在其上执行 for 一样。
  • 确实如此,我不得不尝试。即使我学到了新东西也不喜欢它:-)

标签: python


【解决方案1】:

脚本运行时是否正在写入文件?

除了悬空文件句柄和可能应该通过f.close() 或f.flush() 清空的缓冲区之外,我看不出代码中有任何错误。

但是,您可以通过不使用 list(filehandle) 而是将其替换为 for 循环来改进您的代码。正如 cmets 可能注意到的那样,这不是一种常见的方式。

另外,因为您替换了指向文件句柄 currentStepFile 的变量,您的代码将不得不等待垃圾收集将其关闭。

如果您还在代码的其他地方执行此操作,则很可能是将来出现此问题或其他问题的原因。

【讨论】:

  • 嗨,Unode。澄清一下,“悬空文件句柄”是什么意思?您是否怀疑问题出在使用列表而不是循环,或者因为我没有关闭文件句柄,或两者兼而有之?
  • @gwilymh 悬空文件句柄是用f = open(filename) 打开文件而不用f.close() 关闭它的结果。由于 Python 缓冲写入文件以避免在每次 f.write() 操作时撞到磁盘,因此当您的代码的其他部分尝试读取文件时,文件可能不完整。
  • 谢谢Unode。关闭所有打开的文件句柄似乎已经解决了这个问题。
猜你喜欢
  • 2015-01-20
  • 2022-06-24
  • 2016-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-10
  • 1970-01-01
相关资源
最近更新 更多