在python

时间:2017-01-16 09:14:16

标签: python loops filenames

我有几百个大文件(基于行号)。 我正在尝试使用循环编写代码。 首先循环读取文件夹中的bigfile, 第二,它将创建一个与正在读取的文件名相同的文件夹 最后它会将文件切片到创建的同一文件夹中。 此循环应迭代文件夹中存在的所有bigfiles。 我的代码如下:

import glob
import os
os.chdir("/test code/")
lines_per_file = 106
sf = None
for file in glob.glob("*.TAB"):
    with open(file) as bigfile:
        for lineno, line in enumerate(bigfile):
            if lineno % lines_per_file == 0:
                if sf:
                    sf.close()
                    sf_filename = '/test code/201511_sst/sf_{}.txt'.format(lineno + lines_per_file)
                    sf = open(sf_filename, "w")
                    sf.write(line)
                    if sf:
                        sf.close()

我得到的输出如下:

In [35]: runfile('/test code/file_loop_16Jan.py', wdir='/test code')
In [36]:

我需要一些循环文件的指导,以便我可以实现它。我认为没有错误意味着我错过了什么! 请有人帮帮我!

2 个答案:

答案 0 :(得分:2)

sf在开始时设置为None,因此您永远不会进入if sf循环:任何地方都没有输出任何输出文件。

此外,当您关闭文件时,您必须再次将sf设置为None,否则您将对已关闭的文件"进行操作。再次关闭时。

但那不会做你想要的。您想拆分文件,所以这样做:

        if lineno % lines_per_file == 0:
            # new file, close previous file if any
            if sf:
                sf.close()
            # open new file
            sf_filename = '/test code/201511_sst/sf_{}.txt'.format(lineno + lines_per_file)
            sf = open(sf_filename, "w")
        # write the line in the current handler
        sf.write(line)
  • 开始时遇到第一个if:好。由于sfNone,因此不会调用close(最佳)
  • 然后用新文件名
  • 打开文件
  • 现在该行被写入新的文件句柄中(你必须在每次迭代时写一行,而不仅仅是当模数匹配时)

在下一次迭代中,当模数匹配时,前一个文件关闭,并创建一个带有新文件名的新句柄。

退出循环时不要忘记关闭最后一个文件句柄:

if sf:
    sf.close()

我还没有测试过,但逻辑就在这里。如果您有后续问题,请注释我将编辑我的帖子。

除此之外:另一个问题是,如果有超过1个大*.TAB个文件,分割文件将被覆盖。为避免这种情况,我会在输出文件中添加输入文件basename(例如,lineno在每个循环中重置):

sf_filename = '/test code/201511_sst/{}_sf_{}.txt'.format(os.path.splitext(os.path.basename(file))[0]),lineno + lines_per_file)

你也可以通过存储结束lineno来计算行偏移量。它取决于你

答案 1 :(得分:0)

由于您已经使用with语句来读取文件,因此您也可以使用它来写入文件,这样就不需要显式关闭文件对象了。看到这些链接。

https://docs.python.org/2/reference/compound_stmts.html#with https://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects

你可以这样做:

with open(file,"w") as sf:
    // read/write file content and do your stuff here