【问题标题】:error reading files while looping through files in a directory in python2.7在python2.7的目录中循环文件时读取文件时出错
【发布时间】:2012-08-28 22:15:16
【问题描述】:

我从昨天开始就在谷歌上搜索这个问题,但无济于事;

当我遍历一个目录中的多个文件并处理该循环中每个文件的行时,我总是关闭,但似乎 python 正在打开同一内存空间中的所有文件,所以当我遍历一个文件我从以前打开的文件中检索所有记录,就好像它们在一个指针数组中一样。 . . .wtf?

    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()

根据要求,这是我的 i/o 示例:

输入文件1:
AB00007737106517 COSTCLASSU275
C000000010031932155750539976333693187714
C000000010031932155750539976105307608239

文件2:
AB00007736638744 COSTCLASSU275
C000000010030284907699012480608351468369
C000000020030284907699012480751885101503

文件3:
AB00007737148207 COSTCLASSU275
C000000010032271716759259098738354718484
C000000020032271716759259098394986919513

所需的输出文件 1:
AB00007737148207 COSTCLASSU275
|C000000010031932155750539976079292077121
|C000000010031932155750539976126217711213

文件2:
AB00007736638744 COSTCLASSU275
|C000000010030284907699012480968864628712
|C000000020030284907699012480294550195814

文件3:
AB00007737106517 COSTCLASSU275
|C000000010032271716759259098216262704445
|C000000020032271716759259098085462231948

当前输出文件1:
AB00007737148207 COSTCLASSU275
|C000000010031932155750539976079292077121
|C000000010031932155750539976126217711213

文件2:
AB00007736638744 COSTCLASSU275
|C000000010031932155750539976079292077121
|C000000010031932155750539976126217711213
.
.
.
|C000000010030284907699012480968864628712
|C000000020030284907699012480294550195814
文件3:
AB00007737106517 COSTCLASSU275
|C000000010031932155750539976079292077121
|C000000010031932155750539976126217711213
.
.
.
|C000000010030284907699012480968864628712
|C000000020030284907699012480294550195814
.
.
.
|C000000010032271716759259098216262704445
|C000000020032271716759259098085462231948

【问题讨论】:

  • 这似乎是 with statements 的设计目的。 IE。获取所有文件名的列表,并在 with 语句中打开每个文件名...
  • 正如我所写的,我不明白为什么 unscrambledCodes 不会为第二个 readline() 做正确的事情。也许显示一些实际输出,以及您的预期将有助于澄清实际问题是什么?
  • @jszakmeister 你不知道我想怎么做,但是数据太敏感我会被解雇:-(,可能也会被送上法庭
  • @pyruva 您可以尝试制作一个可以共享的示例(三个文件带有 foo 和 bar 带有数字)。我真的怀疑问题出在哪里。

标签: python file file-io python-2.7 tuples


【解决方案1】:

普遍的共识是使用open而不是file。我会从那开始。

其次,尝试对打开的文件进行生成器理解,因为它更容易(下一个方法将返回换行符)为 new_file=[x.strip() for x in unscrambledCodes)] ,然后执行您必须执行的任何其他操作,例如 new_file=["|"+line for line in new_file[:-1]] 和 @987654323 @

正如上面其他人指出的那样,您可能想尝试 with 关键字(即使它会带来另一个级别的缩进),例如

with open("....","r") as in_file, open("...","w") as out_file:

`'''.... do your stuff'''`

【讨论】:

  • 今天早上改为归档,看看是否会改变什么。 . .它不是
  • 感谢with的东西,我想我昨晚试过了,但无济于事。 . .不过再试一次也没坏处。我会告诉你会发生什么
【解决方案2】:

是的,unscrambledCodes.readline() 将一次读取文件的一行,递增到下一行,直到读入整个文件。

你可以使用类似的东西:

content = unscrambledCodes.readlines()

它将每一行读入一个数组。然后您可以遍历内容,并根据需要更新行。

另外,我一般用file()代替

myFile = open('filename.txt','r')
content = myFile.readlines()
myFile.close()

【讨论】:

  • 我今天早上才把它改成 file(),它们都失败了,还有你的解决方案,对不起
  • 我不确定我是否正确理解了这个问题。当您设置output = '\n'.join(_list) 时,_list 是否包含以前迭代的项目?
  • 嗨,Sam,不,一旦我进入 while 循环,以前的记录就开始出现在 var 行中
猜你喜欢
  • 2020-01-12
  • 2020-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多