Last active
October 19, 2015 14:47
-
-
Save m-note/835fe726bd4abf27e5d5 to your computer and use it in GitHub Desktop.
ファイルを、テキストファイルに改行で記述していたときなどに、書き込もうとしているファイル名が既にあるかをチェックして、なければ新しいファイル名を作る。ファイルのリストの取得などを別の方法でできれば、応用も広がる。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 実際は with open("downloaded_tag.txt", "r") as file_read: などとして、 | |
# 下のコードでlistになっているところを、file_readに置き換える | |
save_base = "a" | |
file_num = 1 | |
save_path = save_base + "_" + str(1) | |
flag = False | |
while flag == False: | |
check = False | |
for line in ["a_1", "b_1", "a_4", "a_2", "c_1", "d_1", "a_3", "e_1"]: | |
# ここでこのリストは、ファイル名 / パスのリストの代替として使っている | |
if re.search(save_path, line) != None: | |
file_num += 1 | |
save_path = save_base + "_" + str(file_num) | |
print("File already exists. New file is no.", file_num, save_path) | |
check = True | |
if check == False: | |
print("flag", save_path) | |
# 被るものが1つもなければwhile文を抜け出す | |
flag = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment