ÆÄÀ̽ã, Ãâ·ÂµÈ ³»¿ëÀ» ÆÄÀÏ¿¡ ÀúÀåÇÏ´Â ¹æ¹ý Áú¹®

   Á¶È¸ 9594   Ãßõ 0    

안녕하세요

아래와 같이 출력된 내용을 파일로 저장을 하고 싶은데 잘 안되서 조언 부탁드립니다.



ªÀº±Û Àϼö·Ï ½ÅÁßÇÏ°Ô.
dogbull 2019-01
¸ðµç Ç¥ÁØÃâ·ÂÀ» ÇϳªÀÇ ÆÄÀÏ·Î º¸³»±æ ¿øÇÑ´Ù¸é ¾Æ·¡¿Í °°ÀÌ ÇÏ¸é µÇ°Ú½À´Ï´Ù.
import sys
sys.stdout=open('log.txt', 'w')
print(1)
print(2)
     
³×ÀÌÃÄ 2019-01
´Ý´Â°Å´Â sys.stdout.close() ¶ó°í ÇÏ¸é µÇ³ª¿ä??
Çغ¸´Ï±î ÀÛµ¿Àº Á¦´ë·Î Çϴ°Š°°Àºµ¥ ¿¡·¯ ¸Þ¼¼Áöµµ ¶ß´Â...

Exception in callback BaseAsyncIOLoop._handle_events(12, 1)
handle: <Handle BaseAsyncIOLoop._handle_events(12, 1)>
Traceback (most recent call last):
  File "/usr/local/anaconda3_5.2.0/lib/python3.6/asyncio/events.py", line 145, in _run
    self._callback(*self._args)
  File "/usr/local/anaconda3_5.2.0/lib/python3.6/site-packages/tornado/platform/asyncio.py", line 117, in _handle_events
    handler_func(fileobj, events)
  File "/usr/local/anaconda3_5.2.0/lib/python3.6/site-packages/tornado/stack_context.py", line 276, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/anaconda3_5.2.0/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 450, in _handle_events
    self._handle_recv()
  File "/usr/local/anaconda3_5.2.0/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 480, in _handle_recv
    self._run_callback(callback, msg)
  File "/usr/local/anaconda3_5.2.0/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 432, in _run_callback
    callback(*args, **kwargs)
  File "/usr/local/anaconda3_5.2.0/lib/python3.6/site-packages/tornado/stack_context.py", line 276, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/anaconda3_5.2.0/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/usr/local/anaconda3_5.2.0/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 239, in dispatch_shell
    sys.stdout.flush()
ValueError: I/O operation on closed file.
          
dogbull 2019-01
import sys
sys.stdout=open('log.txt', 'w')
sys.stdout.close()
print(1)
print(2)

sys.stdout.close() ¸¦ È£ÃâÇÏ¸é ±× ÀÌÈÄ¿¡ sys.stdout¿¡ ´ëÇÑ Àбâ/¾²±â ÀÛ¾÷Àº,
¸»¾¸ÇϽŠValueError: I/O operation on closed file °¡ ¹ß»ýÇÕ´Ï´Ù.
Á¦°¡ ¾ð±ÞÇÑ ³»¿ëÀº ´Ü¼øÈ÷ µ¿ÀÛ ¹æ½ÄÀ» º¸¿©ÁÖ±â À§ÇÑ °ÍÀÏ»Ó,
¾Æ·¡ timeleaf ´ÔÀÇ ¸»¾¸´ë·Î logging ¸ðµâÀ» »ç¿ëÇÏ´Â°Ô ´õ ÁÁÀº ¹æ¹ýÀÔ´Ï´Ù.
               
³×ÀÌÃÄ 2019-01
´äº¯ °¨»çÇÕ´Ï´Ù.

import sys

temp = sys.stdout  <---
sys.stdout = open("log.dat", "w")
print("test1234")
print("t1111234")
print("zlvxxjv393")

sys.stdout.close()
sys.stdout = temp  <---  À̰Ÿ¦ ÇØÁÖ¸é µÈ´Ù°í https://code.i-harness.com/ko-kr/q/31ccb8 ¿©±â º¸°í µû¶óÇغýÀ´Ï´Ù.

=====================
with openÀ» ¾µ ¶§µµ ¾Æ·¡Ã³·³ Çϴϱî Àߵdz׿ä~

temp = sys.stdout  <---
with open("log.dat", "w") as sys.stdout:
    print("test1234")
    print("t1111234")
    print("zlvxxjv393")

    sys.stdout = temp  <---


±×¸®°í loggingµµ Çѹø ºÃ´Âµ¥, ¾ÆÁ÷ Ãʺ¸¶ó ¹«¼­¿ö º¸¿©¼­ õõÈ÷ º¸·Á°í ÇÕ´Ï´Ù
dogbull 2019-01
Ãâ·Â¸¶´Ù ´ÙÀº ÆÄÀÏ·Î º¸³»±æ ¿øÇÑ´Ù¸é ¾Æ·¡¿Í °°ÀÌ ÇÏ¸é µÇ°Ú½À´Ï´Ù.
fp1=open('log1.txt', 'w')
fp2=open('log2.txt', 'w')
print(1, file=fp1)
print(2, file=fp2)
dogbull 2019-01
ÄÚµå ¼öÁ¤ ¾øÀÌ ¾Æ·¡¿Í °°ÀÌÇصµ µÇ°Ú³×¿ä.(ÇÏÁö¸¸ save_result °ü·Ã ±¸¹®Àº Áö¿ì¼Å¾ßÇÒ°Í °°¾Æ¿ä.)
python ½ºÅ©¸³Æ®¸í.py > log.txt
     
³×ÀÌÃÄ 2019-01
´äº¯ °¨»çÇÕ´Ï´Ù.

¸»¾¸ÇØÁֽŴë·Î Çغ¸´Ï Àß µÇ¾ú½À´Ï´Ù~
±×·±µ¥ Àú ÇÔ¼ö¸íÀ» °¡Á®´Ù°¡ ¾µ ¼ö´Â ¾ø³ª¿ä?
È­¸é¿¡ Ãâ·Âµµ µÇ¸é¼­, ÀúÀåµµ µ¿½Ã¿¡ µÇ°Ô ÇÏ°í½Í°Åµç¿ä~
timeleaf 2019-01
import logging »ç¿ëÇÏ¸é µÉ °Í °°¾Æ¿ä
print ´ë½Å¿¡ logging.info »ç¿ëÇÏ°í¿ä
https://docs.python.org/ko/3/howto/logging.html
     
³×ÀÌÃÄ 2019-01
´äº¯ °¨»çÇÕ´Ï´Ù.

¾ÆÁ÷ ¿Í´êÁö ¾ÊÁö¸¸, Àß Âü°íÇؼ­ °øºÎÇغ¸°Ú½À´Ï´Ù~


QnA
Á¦¸ñPage 1386/5680
2015-12   1484414   ¹é¸Þ°¡
2014-05   4947721   Á¤ÀºÁØ1
2019-01   7589   ÅëÅë9
2019-01   2704   ±è¿µ±â
2019-01   3274   ±è°Ç¿ì
2019-01   3239   Àü¼³¼ÓÀǹ̡¦
2019-01   4183   À¯·ÉEÁ¤ÇØÁØ
2019-01   18557   esca
2019-01   2869   µð¾Æ°í¼ö
2019-01   3457   ¾ö¸¶À÷Èë¸Ô¾î
2019-01   3205   ¼­¹öÇÏ°ÅÆÄ
2019-01   3596   TLaJ3KtYGr
2019-01   4858   song05
2019-01   3629   1CPUÁöâÈÆ
2019-01   4092   ¼­¹öÇÏ°ÅÆÄ
2019-01   3893   ¸Þ¸ð¸®´Ô
2019-01   2650   ¿è¿è
2019-01   3130   nelson1945
2019-01   2772   newretrowave
2019-01   2980   ³ª³Ê¿ì¸®
2019-01   5572   »¡°­Åä³¢
2019-01   7422   ´Ù·ÕÀÌ