为文件夹中的文件运行Python脚本

时间:2019-06-05 10:37:08

标签: python-3.x

一个文件夹中有15个文本文件,我正在尝试提取每个文件的某些部分并将它们输出到新文件中。

通过更改文件名并将每个文件附加到输出文件中,我就可以分别提取每个文件,但这意味着将相同的代码复制15次,每次更改文件名。

import glob,os

lst = []
filelist=glob.glob  ('/C:/Users/bridaly/Documents/PythonTest/Python_Test_ENdata_3080_v20150914/input/*')

for file in filelist:

    if os.path.isfile(file):

        for line in filelist:
            line = line.strip()
            if not (
                line.startswith("APPEND") or line.startswith("_") or
                line.startswith("SAP") or line.startswith("~") or
                line.startswith("INCLUDE")  or line.startswith("ABAP")
                or line.strip() == "" or line.startswith("Field") or
                line.startswith("Short")
                ) :     

                y=line.replace('     ',' ')
                #print(y)

                z = y.replace('X','')
                #print(z)

                w = "|".join(z.split())
                #print(w)

                x = w.split("|",3)[:4]
                #print(x)
                x.insert(0,'./input/01BKPF')
                #print(x)

                if len(x) >=4:


                    t = [s.replace('|',' ') for s in x]
                    #print(t)
                    print("|".join(t))
                    lst.append("|".join(t))
#Output Script

output_file = open('Output_Final.txt', 'w')
for l in lst:
    output_file.write(l)
    output_file.write('\n')
output_file.close()

“”“

输出应提取代码中写入的内容,但要提取每个文件的内容,并将其附加到输出文件中。通过复制代码15次,我已经获得了正确的输出,但是我只想使用一次,因为它效率更高。

0 个答案:

没有答案