본문 바로가기

stdout3

Python 파일에 로그를 기록하고 표준 출력에 출력하기 위한 로거 설정, logger configuration to log to file and print to stdout 질문 I'm using Python's logging module to log some debug strings to a file which works pretty well. Now in addition, I'd like to use this module to also print the strings out to stdout. How do I do this? In order to log my strings to a file I use following code: import logging import logging.handlers logger = logging.getLogger("") logger.setLevel(logging.DEBUG) handler = logging.handlers.RotatingF.. 2023. 9. 9.
Python 바이트를 문자열로 변환하세요., Convert bytes to a string 질문 외부 프로그램의 표준 출력을 bytes 객체로 캡처했습니다: >>> from subprocess import * >>> command_stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0] >>> >>> command_stdout b'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file1\n-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file2\n' 그것을 일반적인 Python 문자열로 변환하여 다음과 같이 출력하려고합니다: >>> print(command_stdout) -rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file1 -rw-rw-r-- 1 .. 2023. 5. 5.
Python 프로그램을 실행하거나 시스템 명령을 호출하는 방법은 무엇인가요?, How do I execute a program or call a system command? 질문 프로그램을 실행하거나 시스템 명령을 호출하는 방법은 무엇인가요? Python에서 쉘 또는 명령 프롬프트에 입력한 것처럼 외부 명령을 호출하는 방법은 무엇인가요? 답변 subprocess 모듈을 사용합니다. 이 모듈은 표준 라이브러리에 포함되어 있습니다: import subprocess # 간단한 명령어의 경우 subprocess.run(["ls", "-l"]) # 복잡한 명령어의 경우, 많은 인수를 사용하는 경우, 문자열 + `shell=True`을 사용합니다: cmd_str = "ls -l /tmp | awk '{print $3,$9}' | grep root" subprocess.run(cmd_str, shell=True) subprocess.run은 os.system보다 더 유연합니다. (std.. 2023. 5. 4.