1

私はさらに別の python パージ スクリプトを書いています。これは非常に古い bash スクリプトを大量の find -delete に置き換えており、ビデオ バックエンドを消去するのに最大 9 時間かかります。

私はそれらがスタック上またはGoogleの右にたくさんあることを知っていますが、私にはいくつかの制約があり、貧弱/非効率的なコードを見つけたものを書く必要がありました。

次のディレクトリ構造を検討してください。

/data/channel1/video_800/0001/somefile_800_001.ts
/data/channel1/video_800/0001/somefile_800_002.ts
/data/channel1/video_800/0002/somediffile_800_001.ts
/data/channel1/video_800/0002/somediffile_800_002.ts
/データ/channel1/video_800.m3u8
/data/channel1/video_900/0001/someotherfile_900_001.ts
/data/channel1/video_900/0002/afile_900_001.ts
/data/channel1/video_900/0003/bfile_900_001.ts
/data/channel1/video_900/0003 /cfile_900_001.ts
/data/channel1/video_900.m3u8

/data/channel2/video_800/0001/againsomefile_800_001.ts
/data/channel2/video_800/0001/againsomefile_800_001.ts
/data/channel2/video_800.m3u8

/data/sport_channel/video_1000/0001/somefile.ts /data/sport_channel/video_1000/0001/somefile2.ts

私が最初に興味を持ったのは、チャネル*とスポーツ*のルールがあるため、チャネル名です。

2 つ目は、ビットレートに等しいビデオディレクトリの終わりです... 800、900、1000 は、保持日数が異なる可能性があるためです。

最後に、すべてを調べて、ビットレートと拡張子に基づいてファイルを削除します。

次のコードは機能しますが、非常に複雑であり、あまりPythonicではないと確信しています。この場合、私が最も気にかけているのはパフォーマンスであるため、これを行うためのより効率的な方法があると確信しています。for ループ内に for ループをスタックすると、設計が不十分なだけでなく、pymode で「find_files」が複雑すぎる [mccabe] というメッセージが表示されます。

** コード例から remove 関数を除外しましたが、os.rmdir と os.remove を使用することを除いて、単純な試行です。

コードを改善するためのすべての提案を受け入れます。

ありがとう!

#!/usr/bin/python

import os
import time
import fnmatch

path = '/data'
debits_short = ['200', '700', '1000', '1300', '2500']
debits_long = ['400', '1800']

def find_files(chan_name, debits, duration):

    time_in_secs = time.time() - (duration * 24 * 60 * 60)

    # List channel
    for channel in os.listdir(path):

        # Match category channels
        if fnmatch.fnmatch(channel, chan_name):

            # Go through bitrates
            for debit in debits:

                # Channel path now the default search path
                channel_path = path + channel

                # Walk through channel path to match bitrate files
                for root, dirs, files in os.walk(channel_path, topdown=False):
                    for filename in files:

                        # Remove files that contain _bitrate_ and end with ts
                        if '_' + debit + '_' in filename:
                            if filename.endswith('.ts'):
                                if os.path.isfile(os.path.join(root, filename)):
                                    if os.stat(os.path.join(root, filename)).st_mtime <= time_in_secs:
                                        remove(os.path.join(root, filename))

                        # Remove playlist files that contain bitrate.m3u8
                        if filename.endswith(debit + '.m3u8'):
                            if os.path.isfile(os.path.join(root, filename)):
                                if os.stat(os.path.join(root, filename)).st_mtime <= time_in_secs:
                                    remove(os.path.join(root, filename))

                    # Remove empty dirs
                    for dir in dirs:
                        if not os.listdir(os.path.join(root, dir)):
                            remove(os.path.join(root, dir))


find_files('channel*', debits_long, 3)
find_files('sport*', debits_short, 7)
4

1 に答える 1