
9/11 1, 2, 3세션
C:\Users\User> from pathlib import Path
> print(Path.home())
Path 라이브러리
home() : 홈 디렉터리 확인cwd() : 현재 작업 디렉토리 확인cwd().glob('*') : 현재 작업 디렉터리 안의 모든 내용, 모든 확장자 확인# 파일 열기
> f = open('MyFile.txt', 'w')
# 파일 쓰기
> f.write('안녕하세요?\n')
# 파일 닫기
> f.close()
폴더(디렉터리) 만들기
> Path('Files').mkdir(exist_ok=True)
# 파일 열기
> f = open('MyFile.txt', 'r')
# 내용 읽기
> print(f.read())
# 파일 닫기
> f.close()
# 파일 열기
> f = open('MyFile.txt', 'a')
# 내용 추가
> f.write('만나서 반갑습니다!')
# 파일 닫기
> f.close()
안녕하세요?
만나서 반갑습니다!
# 오류 핸들링
> try:
f = open('MyFile.txt', 'x')
f.write('만나서 반갑습니다!')
f.close()
> except FileExistsError: # 이미 파일이 있으면
print('같은 이름의 파일이 있습니다.')
> else: # 그렇지 않으면
print('파일 쓰기 성공했습니다.')
> finally: # 마무리
print('수고하셨습니다.')
writelines() 메서드> f.writelines(리스트)
readlines() 메서드> result = f.readlines()
readline() 메서드wordcloud 패키지# wordcloud 패키지 설치
!pip install wordcloud
# 텍스트 원문
text = 'I am happy to join with you today in what will go down in history as the greatest demonstration for '
# 공백을 구분자로 하여 단어 단위로 자르기
wordList = text.split()
# 중복 단어 제거, 딕셔너리에 단어별 개수 저장
worduniq = set(wordList)
wordCount = {}
for w in worduniq:
wordCount[w] = wordList.count(w) # 중복이 있는 리스트에서 개수 가져오기
# 제외 대상 조사
del_word = ['the','a','is','are', 'not','of','on','that','this','and','be','to', 'from']
# 제외하기
for w in del_word:
if w in wordCount:
del wordCount[w]
# 라이브러리 불러오기
import matplotlib.pyplot as plt
from wordcloud import WordCloud
%config InlineBackend.figure_format='retina' #고해상도 설정
# 워드 클라우드 만들기
wordcloud = WordCloud(font_path = 'C:/Windows/fonts/HMKMRHD.TTF',
width=2000,
height=1000,
background_color='white').generate_from_frequencies(wordCount) #딕셔너리를 기반으로 그려주세요
# 표시하기
plt.figure(figsize=(12, 6))
plt.imshow(wordcloud)
plt.axis("off")
plt.tight_layout(pad=0)
plt.show()
# 파일로 저장
wordcloud.to_file('wordcloud1.png')

# 라이브러리 불러오기
import numpy as np
from PIL import Image
# 이미지 불러오기
masking_image = np.array(Image.open('human.jpg'))
WordCloud(mask=masking_image) 추가
