为同一扩展名的许多文件运行脚本,并将结果写入文本文件

时间:2016-03-01 19:24:14

标签: python python-2.7 glob

我试图编写一个脚本来从目录中的多个文件中提取数据,扩展名为" .tp6"然后将所有数据写入单个文本文件。

它能够正确地从每个文件中获取数据并将其打印到终端,但我还没有能够通过'每个数据指向另一个将其写入文本文件的函数。

有什么想法吗?谢谢!

import glob
import os
import Tkinter
import tkFileDialog

root = Tkinter.Tk()
root.withdraw()
dir_path = tkFileDialog.askdirectory()
os.chdir(dir_path)


def main():
    for file_path in glob.glob('*.tp6'):
        uncovext(file_path)

def main2():
    for file_path in glob.glob('*.tp6'):
        totext(uncovext)


#find and print data from each .tp6 file - this part works correctly
def uncovext(file_path):
    for line in open(file_path):
        if line.startswith(' UNCONVOLVED INTEGRATED RADIANCE'):
            text = line[36:47]
            number = float(text) * 10000
            print('%.3f' % number)

def totext(uncovext):
    with open("output.txt", "a") as f:
        f.write(uncovext)
        f.close()

if __name__ == '__main__':
    main()
    main2()

2 个答案:

答案 0 :(得分:0)

我认为这是一个命名问题:例如,如果你将totext函数的输入参数更改为p_uncovext,它应该可以工作。你还需要调用函数来循环文本。

import glob
import os
import Tkinter
import tkFileDialog

root = Tkinter.Tk()
root.withdraw()
dir_path = tkFileDialog.askdirectory()
os.chdir(dir_path)

def main():
    for file_path in glob.glob('*.tp6'):
        uncovext(file_path)

#find and print data from each .tp6 file - this part works correctly
def uncovext(file_path):
    for line in open(file_path):
        if line.startswith(' UNCONVOLVED INTEGRATED RADIANCE'):
           text = line[36:47]
           number = float(text) * 10000
           totext('%.3f' % number)

def totext(p_uncovext):
    with open("output.txt", "a") as f:
       f.write(p_uncovext)
       f.close()

if __name__ == '__main__':
    main()

答案 1 :(得分:0)

你有几个问题。首先uncovext不保存它从输入文件中解析的数据。打印到屏幕后,它就被扔掉了。您可以将其收集到列表中并返回以进行进一步处理。然后,您在第二个函数中调用编写器,而main无法让main2知道数据是什么。

一个简单的修复方法是调用uncovext并使用其结果调用totext的单个函数。

import glob
import os
import Tkinter
import tkFileDialog

root = Tkinter.Tk()
root.withdraw()
dir_path = tkFileDialog.askdirectory()
os.chdir(dir_path)


def main():
    for file_path in glob.glob('*.tp6'):
        totext(uncovext(file_path))

#find and print data from each .tp6 file - this part works correctly
def uncovext(file_path):
    output = []
    for line in open(file_path):
        if line.startswith(' UNCONVOLVED INTEGRATED RADIANCE'):
            text = line[36:47]
            number = float(text) * 10000
            output.append('%.3f\n' % number)
    return output

def totext(uncovext):
    with open("output.txt", "a") as f:
        f.writelines(uncovext)

if __name__ == '__main__':
    main()

您还可以将解析器重写为生成器并编写代码,我发现这些代码更加不言自明(但这只是我...)

def main():
    with open('output.txt', 'a') as f:
        for file_path in glob.glob('*.tp6'):
            f.writelines(uncovext(file_path))

#find and print data from each .tp6 file - this part works correctly
def uncovext(file_path):
    for line in open(file_path):
        if line.startswith(' UNCONVOLVED INTEGRATED RADIANCE'):
            text = line[36:47]
            number = float(text) * 10000
            yield '%.3f\n' % number