import sys class Tee(object): def __init__(self, *files): self.files = files def write(self, obj): for f in self.files: f.write(obj.encode('utf-8')) if __name__ == "__main__": f = open('logfile.txt', 'w') original = sys.stdout sys.stdout = Tee(sys.stdout, f) print "test" # This will go to stdout and the file out.txt #use the original sys.stdout = original print "This won't appear on file" # Only on stdout f.close()
위 코드는 logfile.txt를 만들고 "test"를 그 곳에 출력 (log출력 리다이렉션)합니다.
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
8 | pybind11 에 대해서 | makersweb | 2023.07.23 | 2184 |
7 | 명령어 한줄로 내컴퓨터를 웹서버로 | 운영자 | 2019.01.25 | 3555 |
» | print를 로그파일로 생성하기 (log출력 Redirection) | makersweb | 2015.03.18 | 5704 |
5 | 외부 프로그램 실행 | pjk | 2015.02.10 | 15374 |
4 | 우분투에 Python새 버전 설치 사용법 | pjk | 2015.02.10 | 3253 |
3 | Python 문자열 관련 함수 레퍼런스 | pjk | 2014.08.29 | 6795 |
2 | 다른 디렉터리의 파일(모듈) import 하기 | pjk | 2014.08.22 | 4697 |
1 | Python으로 작성된 프로그램을 윈도우응용프로그램(exe)으로 빌드하기 | pjk | 2014.08.03 | 8095 |