<p>我从昨天开始在谷歌上搜索这个问题,但没有结果</p>
<p>当我循环访问一个目录中的多个文件,并处理该循环中每个文件的行时,我总是关闭,但似乎python正在打开同一个内存空间中的所有文件,所以当我遍历一个文件时,我从以前打开的文件中检索所有记录,就好像它们在一个指针数组中。世界跆拳道联盟?在</p>
<pre><code> import os
import sys
import glob
import string
import cPickle
path2 = './'
columnShuffleTable = loadColumnTable('myTable') #func previously defined
codeScrambleTable = loadScrambleTable('theirTable') #func previously defined
pathToFiles2 = glob.glob(os.path.join(path2, '*.DAT'))
for curFile in pathToFiles2:
_list = ['',]
#this is the variable with which I'm having a problem
unscrambledCodes = file(curFile[-10:], 'r')
#this always yields the actual first line of the file at which I am currently at
line = unscrambledCodes.readline()
_list[0] = '|' + line.strip() #stripping trailing spaces
#the list length at this point always equates to '1', so up to here everything is great
print "list length:", len(_list)
# this always reads the 2nd line of the very first file I loaded. . .wtf?
line = unscrambledCodes.readline().strip()
while(line):
#for unscrambledCodes [my input file]
print "len list: ", len(_list), "infile", unscrambledCodes
nextLine = unscrambledCodes.readline().strip()
if not nextLine:
_list.append('|' + line)
break
else:
_list.append( '|' + line[:-14] + scrambleCode(line[-12:], columnShuffleTable, codeScrambleTable))
#end if
line = nextLine
unscrambledCodes.close()
outfile = open(curFile[-10:-4] + '.Scrambled', 'w')
output = '\n'.join(_list)
outfile.write(output)
outfile.close()
</code></pre>
<p>根据要求,以下是我的i/o示例:</p>
<p>输入文件1:<br/>
AB00007737106517成本类别275<br/>
C000000010031932155750539976333693187714<br/>
C00000001003193215570539976105307608239<br/></p>
<p>文件2:<br/>
AB00007736638744成本类别275<br/>
C00000001003028490769901248068351468369<br/>
C000000020030284907699012480751885101503<br/></p>
<p>文件3:<br/>
AB00007737148207成本类别275<br/>
C0000000100322716759259098738354718484<br/>
C0000000200322716759259098394986919513<br/>
<br/>
所需输出文件1:<br/>
AB00007737148207成本类别275<br/>
|C000000010031932155750539976079292077121<br/>
|C00000001003193215575053997612621771213<br/>
<br/>
文件2:<br/>
AB00007736638744成本类别275<br/>
|C0000000100302849076990124809684628712<br/>
|C000000020030284907699012480294550195814<br/>
<br/>
文件3:<br/>
AB00007737106517成本类别275<br/>
|C0000000100322716759259098216267704445<br/>
|C0000000200322716759259098085462341948<br/>
<br/>
当前输出文件1:<br/>
AB00007737148207成本类别275<br/>
|C000000010031932155750539976079292077121<br/>
|C00000001003193215575053997612621771213<br/>
<br/>
文件2:<br/>
AB00007736638744成本类别275<br/>
|C000000010031932155750539976079292077121<br/>
|C00000001003193215575053997612621771213<br/>
.<br/>
.<br/>
.<br/>
|C0000000100302849076990124809684628712<br/>
|C000000020030284907699012480294550195814<br/>
文件3:<br/>
AB00007737106517成本类别275<br/>
|C000000010031932155750539976079292077121<br/>
|C00000001003193215575053997612621771213<br/>
.<br/>
.<br/>
.<br/>
|C0000000100302849076990124809684628712<br/>
|C000000020030284907699012480294550195814<br/>
.<br/>
.<br/>
.<br/>
|C0000000100322716759259098216267704445<br/>
|C0000000200322716759259098085462341948<br/></p>