ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [파이썬] 파일 만들고 랜덤 개인정보 쓰기
    카테고리 없음 2023. 3. 3. 01:01

    사내 파이썬 강의를 듣고 간략히 주요 코드에 대해 설명을 남김
    참고할 내용은  1) 무작위로 영어 이름 생성,  2) 폴더 만들기,  3) 작업 소요 시간 출력
    ※ 이하는 주요 코드 및 주석인데 직접 입력해보면서 테스트 해봄!

     

    import time
    import random
    import os

    # 시작 시점 시간 기록
    start_time = time.time()

    # 이메일 생성 사용 샘플 글자 정의
    alphabet_samples = "abcdefghizklmnopqrstuvwxyz1234567890"

    # 무작위 선택된 영어 글자 생성 함수
    def random_string(length):
        result = ""
        for i in range(length):
            result += random.choice(alphabet_samples)
        return result


    # 무작위 사람 이름 생성 함수
    def random_name():
        result = ""
        result += random.choice(first_name_samples)
        result += random.choice(middle_name_samples)
        result += random.choice(last_name_samples)
        return result


    # 결과물 저장할 폴더 생성
    os.mkdir("personal_info")


    # 개인정보 파일 자동 생성
    # NUM_SAMPLES 회수만큼 반복
    # 이를테면, NUM_SAMPLES가 100이면 무작위 개인정보 생성을 100회 반복
    for i in range(NUM_SAMPLES):
        # 무작위 사람 이름 생성
        name = random_name()

        # 결과물 파일 이름 정의
        filename = "personal_info/" + str(i) + "_" + name + ".txt"

        # 결과물 파일 생성. 텅 빈 파일 생성
        outfile = open(filename, 'w')

        # 결과물 파일 이름 기재
        outfile.write("name : " + name + "\n")

       ... (생략) ...

        # 결과물 파일 수정 마무리
        outfile.close()


    # 작업 종료 메세지
    print("Process Done.")

    # 작업에 총 몇 초가 걸렸는지 출력
    end_time = time.time()
    print("The Job Took " + str(end_time - start_time)[:3] + " seconds.")

Designed by Tistory.