프로그래밍 언어/Python
Python - glob, pickle / pandas, numpy 설치
dev_tina
2022. 9. 26. 11:21
glob 모듈의 파일 경로 관련 함수
- *, ?, [] 패턴을 이용하여 파일이나 디렉터리 검색
In [17]:
import glob
In [25]:
glob.glob('chap*')
Out[25]:
['chap01_install.ipynb',
'chap02_var_oper.ipynb',
'chap03_lf_loop.ipynb',
'chap04_data_structure.ipynb',
'chap05_function.ipynb',
'chap06_class.ipynb',
'chap08_file.ipynb',
'chapter08']
파일자료 처리
- 특정디렉토리에 있는 여러 파일들 처리
In [26]:
import os # dir or file path
print(os.getcwd()) # D:\Pywork\workspace
txt_data = 'chapter08/txt_data/'
# 지정한 폴더의 목록(file or dir)을 하나씩 넘김
sub_dir = os.listdir(txt_data) # movie 파일 목록 반환
print(sub_dir) # ['first', 'second']
D:\bigdata\jupyter\mjcbook
['first', 'second']

def textPro(sub_dir) :
first_txt = []
second_txt = []
for sdir in sub_dir :
dirname = txt_data + sdir
file_list = os.listdir(dirname)
print(file_list)
In [27]:
# 폴더별 파일 처리 함수
def textPro(sub_dir): # ['first', 'second']
first_txt = [] # first 디렉터리 텍스트 저장
second_txt = [] # second 디렉터리 텍스트 저장
# 디렉터리 구성
for sdir in sub_dir : # ['first', 'second']
dirname = txt_data + sdir # 'chapter08/txt_data/first'
file_list = os.listdir(dirname) # first dir 파일 목록 반환
# 파일 구성
for fname in file_list:
file_path = dirname + '/' + fname # 디렉터리/파일 or 디렉터리
# file 선택
if os.path.isfile(file_path) :
try :
file = open(file_path, 'r') # pos/cv000_29416.txt
if sdir == 'first':
first_txt.append(file.read())
else :
second_txt.append(file.read())
except Exception as e:
print('예외발생 :', e)
finally:
file.close()
else:
print(f'{file_path}는 디렉터리 ~~ 제외')
return first_txt, second_txt
first_txt = [] 일 때,
아래 내용을 쭉 수행하고.
secone_txt = [] 일 때,


In [28]:
first_txt, second_txt = textPro(sub_dir) # ['first', 'second']
print('first_tex 길이 =', len(first_txt)) # first_tex 길이 = 10
print('second_tex 길이 =', len(second_txt)) # second_tex 길이 = 10
chapter08/txt_data/first/test01는 디렉터리 ~~ 제외
chapter08/txt_data/first/test02는 디렉터리 ~~ 제외
chapter08/txt_data/second/test03는 디렉터리 ~~ 제외
chapter08/txt_data/second/test04는 디렉터리 ~~ 제외
first_tex 길이 = 10
second_tex 길이 = 10
In [29]:
# list 결합
tot_texts = first_txt + second_txt
print('tot_texts 길이=', len(tot_texts)) # tot_texts 길이= 20
tot_texts 길이= 20
In [33]:
#tot_texts
객체를 파일에 저장 pickle
- 리스트나 딕셔너리 같은 객체 타입 변수 값을 이진파일(binary file)로 저장
앞에서 만든 건 회사에서 만든 파일인데, 이제 퇴근하고 집에서도 작업해야 한다면!
In [34]:
import pickle
In [35]:
# save : write binary
pfile_w = open("chapter08/data/tot_texts.pck", mode='wb')
pickle.dump(tot_texts, pfile_w)
import pickle
p = open('./data_set.pickle', mode='wb' ) #확장자는 중요하지 않다. 피클로 열거니까. 모드가 중요!
p = open('./data_set.pickle', mode='wb' ) #확장자는 중요하지 않다. 피클로 열거니까. 모드가 중요!

주피터 home 에 보면 data_set 이라는 pickle 파일이 만들어진 것을 볼 수 있다.
In [37]:
# load : read binary
pfile_r = open("chapter08/data/tot_texts.pck", mode='rb')
tot_texts_read = pickle.load(pfile_r)
print('tot_texts 길이 =', len(tot_texts_read)) # 20
#print(type(tot_texts_read)) # <class 'list'>
#print(tot_texts_read)
tot_texts 길이 = 20
외부 패키지 설치 pip
- 설치: pip install package_name
- 삭제: pip uninstall package_name
- 경로: 파이썬설치경로\Lib\site-package
- 가상환경에서 설치하면 그 가상환경에서만 사용 가능
In [39]:
#pip install pandas
설치가 잘 되었는지 확인하기 위해
판다 버전 확인 :
import pandas
pandas.__version__
넘파이 설치 버전 확인 :
import numpy
numpy.__version__