LoginSignup
0
0

More than 3 years have passed since last update.

メモ①

Posted at
test.py
# coding:utf-8

import os
import shutil
import sys

"""
no_args:all
be_args:ID or date or lopg.level
"""
DIST_DIR = '/出力先'


def main():
    args = sys.argv

    # Noneオプションを設定
    for index, item in enumerate(args):
        if args[index] == 'false':
            args[index] = None

    # カレントディレクト配下を検索
    try:
        for root, folders, files in os.walk('test'):
            for file in files:

                # ターゲットファイル
                isTarget = False

                # ファイル名と拡張子を分離
                name, extension = os.path.splitext(file)

                # .logのみ出力対象とする
                if extension == '.log':
                    # コピー元ファイルのフルパスを取得
                    base_file = os.path.join(root, file)

                    # オプション判定様にパスを分解
                    split_base_path = base_file.split('/')

                    # IDオプションの設定
                    if args[1]:
                        if args[1] == split_base_path[1]:
                            isTarget = True

                    # 日付オプションの設定
                    if args[2]:
                        if args[2] in split_base_path[3]:
                            isTarget = True
                        else:
                            isTarget = False

                    # Log.Levelオプションの設定
                    if args[3]:
                        isTarget = False
                        with open(base_file, 'r') as file:
                            line = file.readline()
                            if args[3] in line:
                                isTarget = True

                    # 設定したオプションに合致するファイルをターゲットフォルダにコピー
                    if isTarget:
                        result_file_name = split_base_path[1] + '_' + split_base_path[3]
                        result = DIST_DIR + str(result_file_name)
                        print(base_file + ' ==>> ' + result)

                        # 出力
                        shutil.copy2(base_file, result)
    except Exception:
        print('ファイルのコピーに失敗しました')

if __name__ == '__main__':
    print('#########  処理開始  #########')
    main()
    print('#########  処理終了  #########')



0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0