파일명 랜덤 변경

파일명 랜덤 변경

QA

파일명 랜덤 변경

답변 1

본문

VPS폴더에 모든 파일명을 랜덤하게 변경할수 있을까요...

naver_logo.jpg->qwjieprjp.jpg

PHP나 파이썬으로 구현하면 좋겠습니다

이 질문에 댓글 쓰기 :

답변 1

안녕하세요? ^^

 

질문하신 취지가 폴더의 모든 파일명을 랜덤하게 변경하되

 

확장자는 그대로 유지하는 스크립트가 필요하다는 말씀이시죠? :)

 

 

Stackoverflow에 올라온 Python으로 작성된 코드를 참고하시면 도움이 되실 것 같네요~!

 

https://stackoverflow.com/questions/56193558/randomize-file-names-in-a-specified-folder/56193729#56193729

 

다만 위 코드는 확장자가 변경되는 문제가 있으므로,

 

os.path.splitext()를 활용하여 다음과 같이 수정하셔야 되겠네요 ^^

 


from string import ascii_lowercase
from random import choice, randint, random
import os
 
def randomize_files(dir):
    for f in os.listdir(dir):
        path = os.path.join(dir, f)
        if os.path.isfile(path):
            ext = os.path.splitext(f)[1]
            newname = os.path.join(dir, ''.join([choice(ascii_lowercase) for _ in range(randint(5, 8))]))
            newpath = newname + ext
            os.rename(path, newpath)
            print(f'{path} -> {newpath}')

 

위 코드를 실행시키면 특정 폴더 내의 모든 파일을 확장자를 유지시키면서 

 

파일명을 5 내지 8글자의 랜덤한 영문자로 변환하게 됩니다 ^^

 

 

문제 해결에 조금이나마 도움이 되셨으면 좋겠네요~

 

그럼 굿밤 되세요! :)

답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
전체 16
© SIRSOFT
현재 페이지 제일 처음으로