-
<PYTHON>[에러 처리 (try-except, traceback)]Flower in my dev/Python 2015. 5. 11. 12:53
[try-except] try : #에러 발생 예상 코드 except Exception, msg : #에러 발생시 실행 코드 print "Error is : %s" % str(msg) pass [traceback] import traceback traceback.print_exc() 에러스택을 stdout으로 출력 errStr = traceback.format_exc() 에러스택을 string으로 반환
-
<PYTHON>[subprocess]Flower in my dev/Python 2015. 5. 7. 10:39
파이썬에서 시스템 명령을 실행하는 방법 중에 하나이다. 1 2 3 import subprocess subprocess.call('ls -al', shell=True) cs subprocess를 모듈로 등록하고 call을 불러서 사용하면 끝이다. 결과를 리턴 받고 싶다면 result = subprocess.check_output('명령', shell=True) 그리고 result를 가지고 조건을 걸어 사용하면 끝. ==================== Popen을 사용하는 방법 12345import subprocess r = subprocess.Popen('ls', stdout=subprocess.PIPE).stdout rs = r.read().strip()Colored by Color Scriptercs..
-
<PYTHON>[Unix Time]Flower in my dev/Python 2015. 4. 29. 09:06
1 2 3 4 5 6 7 8 9 10 11 12 13 from time import mktime from datetime import date, timedelta, datetime tDay = date.today() yDay = tDay - timedelta(1) tList = str(yDay).split('-') _start = datetime(int(tList[0]),int(tList[1]),int(tList[2]),0,0,0) _end = datetime(int(tList[0]),int(tList[1]),int(tList[2]),23,59,59) start = int(mktime(_start.timetuple())) end = int(mktime(_end.timetuple())) print "===..
-
<PYTHON>[데몬작성]Flower in my dev/Python 2015. 4. 28. 10:43
http://pydjango.tistory.com/51 에서 퍼온 글입니다. Python으로 Daemon을 만드는 예제 코드 입니다. 일단 해당 예제를 실행시키기 위해서 다운로드 받아야 하는 페키지가 있는데 아래와 같이 받으면 됩니다. python easy_install python-daemon 위의 패키지를 다운로드 하시면 준비는 끝입니다. 본격적으로 코드는 아래와 같습니다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 # To kick off the script, run the following from the python director..
-
<PYTHON>[BeautifulSoup]Flower in my dev/Python 2015. 4. 27. 18:32
설치 pip install BeautifulSoup4 파싱방법 soup.title # The Dormouse's story soup.title.name # u'title' soup.title.string # u'The Dormouse's story' soup.title.parent.name # u'head' soup.p # The Dormouse's story soup.p['class'] # u'title' soup.a # Elsie soup.find_all('a') # [Elsie, # Lacie, # Tillie] soup.find(id="link3") # Tillie URL 추출 for link in soup.find_all('a'): print(link.get('href')) 샘플 웹페이지 파싱 ..
-
<PYTHON>특정 날짜가 지나면 파일을 지우기Flower in my dev/Python 2015. 4. 27. 18:23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #-*- coding:utf-8 -*- import os from datetime import date, timedelta, time, datetime filePath = '경로' days = 60 def listUp(): fileList = os.listdir(filePath) return fileList def date_delta(base_date): yy = int(base_date[:4]) mm = int(base_date[5:7]) dd = int(base_date[8:10]) d = date(yy,mm,dd) return (date.t..
-
<PYTHON>이더레이터?? 제너레이터??Flower in my dev/Python 2015. 4. 23. 11:44
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 >>> a = [0,1,2,3,4,5,6,7,8,9] >>> b = iter(a) >>> b.next() 0 >>> b.next() 1 >>> b.next() 2 >>> b.next() 3 >>> b.next() 4 >>> b.next() 5 >>> b.next() 6 >>> b.next() 7 >>> b.next() 8 >>> b.next() 9 >>> b.next() Traceback (most recent call last): File "", line 1, in StopIteration cs 이더레이터.. 리스트 == [iter()] ==> 이더레이터 이더레이터.next() :..
-
<PYTHON> TCP_Echo_ServerFlower in my dev/Python 2015. 4. 21. 15:10
1 2 3 4 5 6 7 8 9 10 11 12 13 from twisted.internet import protocol, reactor class EchoTCP(protocol.Protocol): def dataReceived(self, data): self.transport.write(data) class EchoFactory(protocol.Factory): def buildProtocol(self, addr): return EchoTCP() print "Starting server." reactor.listenTCP(9001, EchoFactory()) reactor.run() Colored by Color Scripter cs