본문 바로가기
파이썬

파이썬 공부중_여러가지 import os, os.path, glob, sys, math, calendar, string,

by 혜룐 2015. 11. 10.
import os
현재 경로 :
os.getcwd()
특정 경로에 dir : os.listdir(pwd)
파일이름 변경 :
os.rename('fromFilename.ini','toFilename.log')
환경변수를 확인할때 :
os.environ
or os.environ['PATH']
import string
첫 문자만 대문자로 : string.
capitalize('lemon')
특정 문자를 변경 :
string.
replace('kemon','k','l')
import re
sub : 패턴과 일치 하는 문자열 변경
split : 대상 문자열을 입력된 패턴을 구분자로 해 분리
findall : 검색 문자열에서 패턴과 매칭되는 모든 경우를 찾아 리스트로 반환
http://devanix.tistory.com/296
import glob
파일들의 목록을 뽑을때 사용
module.py 스크립트 전문이다.
[lhr0916@tpl-master python]# cat module.py 
#!/usr/bin/python
## import
print '===== 1. import'
def testImport():
import math
a = math.pi
print a
import calendar
print calendar.prmonth(2013,7)
testImport()
## https://wikidocs.net/33
print '===== 2. calendar'
def testCalendar():
yyyy = int(raw_input('year : '))
mm = int(raw_input('month : '))
import calendar
print calendar.prmonth(yyyy,mm)
testCalendar()
print '===== 3. sys + os'
def testSys():
import sys, os
pwd = os.getcwd()
print 'before : ',
os.listdir(pwd)
os.rename('renameTest.ini','renameTest.log')
print 'after : ',os.listdir(pwd)
testSys()
print '===== 4. string'
def testString():
import string
l = string.
capitalize('lemon')
ll = string.
replace('kemon','k','l')
print l , ll
testString()
print '===== 5. re, glob'
def testRe():
import re, glob
p = re.compile('.*d.*r.*')
for i in glob.glob('*'):
m = p.match(i)
if m:
print m.group()
## split
str ='apple : orange, tomato banana : lemon, appletree'
print str, '\t', re.split('[:]+', str)
print str, '\t', re.split(':', str)
print str, '\t',
re.split('[:,]+', str)
## sub
print str, '\t', re.
sub('[:, ]+','@@',str)
## findall
print str, '\t', re.findall('app\w*',str)
testRe()
print '===== 6. webbrowser'
def testWeb():
import webbrowser
url='http://www.daum.net'
webbrowser.open(url)
#testWeb()
print "7. os.environ : system environment variable value"
def knowEnv():
import os
print os.environ
print os.environ['PATH']
knowEnv()