WeekendLife/etc

빅데이터분석기사(BAE) - txt 파일에 print 결과 모아서 확인

2025. 5. 13.



빅데이터분석기사를 처음 공부하다 보면 매번 print 문을 치고, terminal에서 확인하기 귀찮을때가 있는데,

txt 파일로 결과물이 한꺼번에 나와있으면 어떨까?

 

dp.py 파일을 생성하고, code 폴더 하위에 output.txt파일이 생성되도록 했다. vscode를 사용중이라면, ctrl+페이지 업다운으로 쉽게 결과를 확인 가능하리라.

 

dp.py
import os
import sys
import subprocess

def printf(*args):
   
    file_path='code/output.txt'
    mode='a'
    encoding='utf-8'
    sep=' '
    end='\n'

    with open(file_path, 'w', encoding=encoding) as f:
        pass

    message = sep.join(str(arg) + end + end for arg in args)

    # 파일에 저장
    with open(file_path, mode, encoding=encoding) as f:        
        f.write(message + end)

 

 

사용은 이런식으로..

`import dp` 를 통해 모듈을 사전에 호출해두어야 한다.

dp.printf(
    df4.set_index('IDX').join(df5.set_index('IDX')),
    df4.join(df5.set_index('IDX'), on='IDX')
)

 

 

 

결과는 이런식으로 모아서..

 

 

 

 

이상 끝.