Skip to content

Instantly share code, notes, and snippets.

@kotoripiyopiyo
Last active December 29, 2020 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kotoripiyopiyo/6118579cef18aa505f04870aaafde5e5 to your computer and use it in GitHub Desktop.
Save kotoripiyopiyo/6118579cef18aa505f04870aaafde5e5 to your computer and use it in GitHub Desktop.
フォルダの中で連番が飛んでいる箇所を見つける
#! python3
# 連番ファイルの穴を見つけ、リストアップ
import re, os
# 正規表現を作る
snumber_regex = re.compile(r'^sample(\d{3}).txt')
# 基準パスはここ
path = './numbering'
# ファイル名をリストアップ
filenames = os.listdir(path)
# 箱を作る
filename_list = []
# 条件に合うファイル名をリストアップ
for i in filenames:
if snumber_regex.search(i):
filename_list.append(i)
# ファイル名をソート
filename_list.sort()
# filename_listを調べて、連番の飛びがあれば連絡する
num = 1
for i in filename_list:
while i[6:9] != f'{num:03d}':
print(f'ない! sample{num:03d}.txt')
num += 1
print(f'{i}はあります。')
num += 1
@kotoripiyopiyo
Copy link
Author

結果の例:

sample001.txtはあります。
sample002.txtはあります。
sample003.txtはあります。
ない! sample004.txt
sample005.txtはあります。
sample006.txtはあります。
sample007.txtはあります。
ない! sample008.txt
sample009.txtはあります。
ない! sample010.txt
sample011.txtはあります。
sample012.txtはあります。
ない! sample013.txt
sample014.txtはあります。
sample015.txtはあります。
sample016.txtはあります。
sample017.txtはあります。
sample018.txtはあります。
ない! sample019.txt
sample020.txtはあります。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment