본문 바로가기

stderr3

Python 코드의 메소드에서 현재 호출 스택을 출력하세요., Print current call stack from a method in code 질문 파이썬에서는 메소드 내부에서 현재 호출 스택을 출력하는 방법이 있을까요? (디버깅 목적으로) 답변 스택을 가져오는 예제는 traceback 모듈을 통해 다음과 같이 출력됩니다: import traceback def f(): g() def g(): for line in traceback.format_stack(): print(line.strip()) f() # 출력: # File "so-stack.py", line 10, in # f() # File "so-stack.py", line 4, in f # g() # File "so-stack.py", line 7, in g # for line in traceback.format_stack(): 스택을 stderr에 출력하려면 다음을 사용할 수 있습니다:.. 2023. 12. 5.
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 프로그램을 실행하거나 시스템 명령을 호출하는 방법은 무엇인가요?, 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.