【Data Language】/Python

[PY2.7] 라이브러리(Library) - os, sys, pickle, shutil, glob, tempfile 모듈

디비랑 2016. 5. 19. 14:47

/************************************************************************************************

-- Title : [PY2.7] 라이브러리(Library) - os, sys, pickle, shutil, glob, tempfile 모듈

-- Reference : itsdong.com

-- Key word : 라이브러리 library 모듈 module sys os pickle shutil glob tempfile 파일 모드 file mode

************************************************************************************************/


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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
 
# --------------------------------------------
# -- 라이브러리(Library)
# --------------------------------------------
 
# -- 파이썬 라이브러리
'''
    ㅇ sys, pickle, io, os, time, random
    ㅇ glob : 파일 내역을 리스트로 생성하고 처리하는 모듈
    ㅇ thread : 쓰레드 처리 모듈
'''
 
 
# --------------------------------------------
# -- sys 모듈
# --------------------------------------------
import sys
print(sys.argv)
'''
C:\> python test.py aaa bbb ccc
['test.py', 'aaa', 'bbb', 'ccc']
'''
 
# sys.exit()                           # 'ctrl+z' 와 동일, 이후 실행 안됨. 테스트 후 주석 처리.
print("This msg has been printed?")
 
print(sys.path)
 
sys.path.append("c:\User")
 
print(sys.path)                        # c:\users 가 추가된 것을 확인
 
 
# --------------------------------------------
# -- pickle 모듈
#    객체 형태를 그대로 유지해서 파일에 저장하고 불러올 수 있게 하는 모듈.
#    바이너리 형태로 저장.
#    pickle 이용시 반드시 binary 처리 해야. Prefix 'b' 추가.
# --------------------------------------------
listA = ['aa''bb''cccc']
 
import pickle
fp = open("list.txt""wb")            # w인데 'b'붙인것
 
# -- pickle.dump : 파일에 쓰기
pickle.dump(listA, fp)
 
fp.close()                             # 파일에 bianry형태로 뭔가 생성됨
 
# -- pickle.load : 읽기
fp = open("list.txt""rb")
listB = pickle.load(fp)
 
print(listB)
 
fp.close()
 
if listA == listB:
    print('Two list values are same.')
 
 
# --------------------------------------------
# -- os 모듈
# --------------------------------------------
'''
    os.environ : 시스템 환경 변수 값 리턴
    os.chdir : 현재 디렉토리 위치 변경
    os.getcwd : 현재 디렉토리 위치 변경
    os.system("명령어") : 시스템의 유틸리티나 DOS 명령어를 호출
    os.popen("명령어") : 시스템 명령어를 실행 시킨 결과값을 읽기모드의 파일 개체로 리턴
    os.mkdir(디렉토리명) : 해당 디렉토리 생성
    os.rmdir(디렉토리명) : 비어있는 디렉토리 삭제
    os.unlink(파일명) : 파일 삭제
    os.rename(src, dst) : src파일의 이름을 dst이름으로 변경
'''
import os
 
print(os.environ)
print(os.environ["PATH"])
 
aa = os.getcwd()
print(aa)
 
fp = os.popen("dir")
bb = fp.read()
print(bb)
 
 
# --------------------------------------------
# -- shutil 모듈
#    파일을 복사해 주는 모듈
# --------------------------------------------
'''
    shutil.copy(src, dst) : src파일을 dst파일/경로로 복사
'''
import shutil
 
shutil.copy("list.txt""list2.txt")
 
 
# --------------------------------------------
# -- glob 모듈
#    디렉토리에 있는 파일들을 리스트로 만들 때 사용
# --------------------------------------------
import glob
 
'''
>>> import glob
>>> glob.glob("c:/python27/*/exe")
['c:/python27\\python.exe', 'c:/python27\\pythonw.exe', 'c:/python27\\Removepy2exe.exe', 'c:/python27\\w9xpopen.exe']
'''
 
 
# --------------------------------------------
# -- tempfile 모듈
#    임시적으로 파일을 만들어 사용할 때 쓰이는 모듈
# --------------------------------------------
'''
    tempfile.mktemp() : 임시로 파일을 만들어 돌려주는 모듈
    tempfile.TemporaryFile() : 임시적인 저장공간으로 사용될 파일 개체를 리턴(w+b모드)
    mode 설명
    w : 쓰기 모드로 파일 열기
    r : 읽기 모드로 파일 열기
    a : 추가 모드로 파일 열기
    b : 바이너리 모드로 파일 열기
    w+, r+, a+ : 파일을 업데이트 할 용도로 사용
    b는 w, r, a 뒤에 붙여서 사용
    r은 단지 읽기 위해서 사용하는 모드, 포인터는 파일 처음에 위치
    r+ : 읽고 쓰기 모드로 파일 열기, 포인터는 파일 처음에 위치
    w : 쓰기 모드로 파일 열고 파일 없을 때 새롭게 생성, 포인터는 파일 처음에 위치
    w+ : 읽기/쓰기 모드로 파일 열고 파일 없을 때 새롭게 생성, 포인터는 파일 처음에 위치
    a : 쓰기 모드로 파일 열고 파일이 없는 경우 새롭게 생성, 포인터는 파일 끝에 위치
    a+ : 읽기/쓰기 모드로 파일 열고 파일이 없는 경우 새롭게 생성, 포인터는 파일 끝에 위치
    파일 읽기 : r, r+, w+, a+
    파일 쓰기 : r+, w, w+, a, a+
    파일 생성 : w, w+, a, a+
    파일 덮어쓰기 : w, w+
    파일 추가쓰기(앞에) : r+
    파일 추가쓰기(뒤에) : a, a+
'''
import tempfile
fp = tempfile.mktemp()
print(fp)
 
= tempfile.TemporaryFile()           # w+모드로 만들어짐
f.write("hello python!!!")
f.seek(0)
data = f.read()
f.close()
 
print(data)
 

cs