Created
November 25, 2019 17:20
-
-
Save doowonee/213ae0bcd560575bb2b9e3f953b614c6 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
import re, os | |
PATH = 'd:/downloads' | |
regex = re.compile(r'\d{3}-\d{1}') | |
files = [] | |
numbers = [] | |
# r=root, d=directories, f = files | |
for r, d, f in os.walk(PATH): | |
for file in f: | |
full_path = os.path.join(r, file) | |
match = regex.match(file) | |
# 089-4 이런 형태의 이름인지 따지고 | |
if match: | |
# 현존하는 파일명을 집어 넣어 다음 순번을 추측 | |
numbers.append(int(file[0:3])) | |
else: | |
# 100kb 이하는 거른다 | |
if os.path.getsize(full_path) > 1000 * 100: | |
files.append(file) | |
# 정렬 | |
numbers.sort(reverse=True) | |
# print(numbers[0]) | |
print("다음 순번", numbers[0] + 1) | |
print("대상", files) | |
for i, f in enumerate(files): | |
before = os.path.join(PATH, f) | |
after = os.path.join(PATH, f'{numbers[0] + 1:03}-{i+1}.jpg') | |
os.rename(before, after) | |
# 바뀐 파일명 | |
print(after) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment