簡體   English   中英

對按日期順序排列的文件夾中的文件重復執行python腳本

[英]Repeatedly execute python script over files in folder arranged in order of date

我在文件夾中存儲了大量文件。 我需要對每個文件對執行相同的python腳本,並將結果輸出到txt或excel文件中。 如何自動處理python或批處理腳本?
例如。 文件夾中的文件

ap.hdf5
sta.hdf5
ap_20150909_154518_00.hdf5
sta_20150909_154518_00.hdf5
ap_20150909_154530_00.hdf5
sta_20150909_154530_00.hdf5
ap_20150909_154541_00.hdf5
sta_20150909_154541_00.hdf5

這些文件是根據修改后的數據排列的。 我需要對每對執行相同的python腳本,並在文本文件中輸出結果。
例如。

python result.py ap.hdf5 sta.hdf5
python result.py ap_20150909_154518_00.hdf5 sta_20150909_154518_00.hdf5

如何創建可以自動執行該過程的批處理文件? 提前致謝。

編輯:文件夾內的文件略有不同。

ap.hdf5
sta.hdf5
ap_20150909_154518_00.hdf5
sta_20150909_154524_00.hdf5
ap_20150909_154530_00.hdf5
sta_20150909_154536_00.hdf5
ap_20150909_154541_00.hdf5
sta_20150909_154547_00.hdf5

在此,在AP.hdf5之后的幾秒鍾內記錄Sta文件

您可以使用glob模塊獲取以ap開頭的文件列表。 然后,您可以將ap更改為sta以獲得配對文件的名稱(假設始終有一對文件)。 有了這個,您就可以處理它們(就像以前一樣)。

import glob
# iterate over all files starting with ap and ending in .hdf5
for file_a in glob.iglob("ap*.hdf5"):
    # replace the beginning of the filename with sta
    file_b = "sta" + file_a[2:]

    # do your processing (result.py) using file_a and file_b as your pair

如果您確實不想修改result.py來執行此操作,則可以使用以下方法:

import subprocess
import glob
import os.path

with open('output.txt', 'w') as f_output:
    # ap files sorted by modified order
    files = sorted(glob.glob(r'ap*.hdf5'), key=lambda x: os.path.getmtime(x))

    for ap in files:
        path, filename = os.path.split(ap)
        sta = os.path.join(path, 'sta{}'.format(filename[2:]))

        # Do we have an ap/sta pair?
        if os.path.exists(sta):
            # Launch the Python script with the required parameters
            p = subprocess.Popen(['python.exe', 'result.py', ap, sta], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            out, err = p.communicate()
            # Write stdout to a file and stderr to the screen
            f_output.write(out)
            print err
        else:
            print '{} is missing'.format(sta)

這將以日期修改順序為每個ap sta文件對運行Python腳本,並將腳本中的所有輸出寫入output.txt

更新 -根據更新的問題,以下腳本將根據文件名將apsta文件配對。 如果找不到合適的一對,它將停止:

import subprocess
import glob
import os.path
import itertools

def sort_by_ending(filename):
    filename = os.path.split(filename)[1]
    if '_' in filename:
        return filename.split('_')[1:]
    else:
        return [filename]

folder = r'c:\test'

with open('output.txt', 'w') as f_output:
    # ap and sta files sorted by filename ending
    files = sorted(glob.glob(os.path.join(folder, 'ap*.hdf5')) + glob.glob(os.path.join(folder, 'sta*.hdf5')), key=sort_by_ending)

    for ap, sta in itertools.izip(*[iter(files)]*2):
        print "'{}' and '{}'".format(os.path.split(ap)[1], os.path.split(sta)[1])
        # Do we have an ap/sta pair?
        if os.path.split(ap)[1].startswith('ap') and os.path.split(sta)[1].startswith('sta'):
            # Launch the Python script with the required parameters
            p = subprocess.Popen(['python.exe', 'result.py', ap, sta], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            out, err = p.communicate()
            # Write stdout to a file and stderr to the screen
            f_output.write(out)
            print err
        else:
            print '{} is missing'.format(sta)
            break

對於給定的文件名示例,這將輸出以下輸出:

'ap_20150909_154518_00.hdf5' and 'sta_20150909_154524_00.hdf5'

'ap_20150909_154530_00.hdf5' and 'sta_20150909_154536_00.hdf5'

'ap_20150909_154541_00.hdf5' and 'sta_20150909_154547_00.hdf5'

'ap.hdf5' and 'sta.hdf5'
import glob, os

ap_files = glob.glob("outdir/ap*.hdf5") #use glob to get ap files
for ap_file in ap_files:   #walk through the ap file names
  basename = os.path.basename(ap_file)  #split of the base name
  sta_file = "outdir/" + basename.replace("ap","sta",1) #make the sta name
  dothingsto(ap_file, sta_file)  #do whatever you wish to the two files

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM