π‘ μκ°ν΄λ³΄κΈ°
νλ‘κ·Έλ¨ μ¬μ©ν λ μΌμ΄λλ μ€λ₯λ€
μμ κ°λ₯ν μμΈ
- λ°μ μ¬λΆλ₯Ό μ¬μ μ μΈμ§ν μ μλ μμΈ
- μ¬μ©μμ μλͺ»λ μ λ ₯, νμΌ νΈμΆ μ νμΌ μμ
- κ°λ°μκ° λ°λμ λͺ μμ μΌλ‘ μ μ ν΄μΌνλ€
μμ λΆκ°λ₯ν μμΈ
- μΈν°νλ¦¬ν° κ³Όμ μμ λ°μνλ μμΈ, κ°λ°μ μ€μ
- 리μ€νΈμ λ²μλ₯Ό λμ΄κ°λ κ° νΈμΆ, μ μ 0μΌλ‘ λλλ κ²½μ°
- μν λΆκ°μ μΈν°ν리ν°κ° μλ νΈμΆ
- μμΈκ° λ°μν κ²½μ° νμ μ‘°μΉ λ± λμ²κ° νμνλ€
1) μλ νμΌ νΈμΆ -> νμΌ μμμ μλ¦Ό
2) κ²μ μ΄μ μ’ λ£ -> κ²μ μ 보 μ μ₯
β¨ νλ‘κ·Έλ¨ = μ ν, λͺ¨λ μλͺ»λ μν©μ λμ²κ° νμνλ€( Exception Handling )
π try ~ except λ¬Έλ²
try:
# μμΈ λ°μ κ°λ₯ μ½λ
except <Exception Type>:
# μμΈ λ°μμ λμνλ μ½λ
π 0μΌλ‘ μ«μλ₯Ό λλ λ μμΈμ²λ¦¬ νκΈ°
for i in range(10):
try:
print(10 / i)
except ZeroDivisionError:
print('Not divided by 0')
π Built-in Exception( κΈ°λ³Έμ μΌλ‘ μ 곡νλ μμΈ )
π μμΈ μ 보 νμνκΈ°
for i in range(10):
try:
print(10 / i )
except ZeroDivistionError as e:
print(e)
print('Not divided by 0')
π try ~ except ~ else
try:
# μμΈ λ°μ κ°λ₯ μ½λ
except <Exception Type>:
# μμΈ λ°μμ λμνλ μ½λ
else:
# μμΈκ° λ°μνμ§ μμ λ λμνλ μ½λ
for i in range(10):
try:
result = 10 / i
except ZeroDivisionError:
print('Not divided by 0')
else:
print(10 / i)
π try ~ except ~ finally
try:
# μμΈ λ°μ κ°λ₯ μ½λ
except <Exception Type>:
# μμΈ λ°μμ λμνλ μ½λ
finally:
# μμΈ λ°μ μ¬λΆμ μκ΄μμ΄ μ€ν
try:
for i in range(1, 10):
result = 10 // i
print(result)
except ZeroDivisionError:
print('Not divided by 0')
finally:
print('μ’
λ£λμμ΅λλ€')
π raise
raise < Exception Type> ( μμΈμ 보 )
while True:
value = input('λ³νν μ μ κ°μ μ
λ ₯ν΄μ£ΌμΈμ')
for digit in value:
if digit not in '0123456789':
raise ValueError('μ«μκ°μ μ
λ ₯νμ§ μμΌμ
¨μ΅λλ€')
print('μ μκ°μΌλ‘ λ³νλ μ«μ -', int(value))
π assert
assert μμΈμ‘°κ±΄
def get_binary_number(decimal_number):
assert isinstance(decimal_number, int)
return bin(decimal_number)
print(get_binary_number(10))
λλ ν 리( Directory)
νμΌ( File )
π python file I/O
open
ν€μλλ₯Ό μ¬μ©f = open('<νμΌ μ΄λ¦>', 'μ κ·Ό λͺ¨λ')
f.close()
f = open('i_have_a_dream.txt', 'r')
contents = f.read()
print(contents)
f.close()
with open(i_have_a_dream.txt', 'r') as my_file:
contents = my_file.read()
print(type(contents), contents)
π νμ΄μ¬μ File Read
with open('i_have_a_dream.txt', 'r') as my_file:
content_list = my_file.readlines() # νμΌ μ 체λ₯Ό listλ‘ λ°ν
print(type(content_list)) # Type νμΈ
print(content_list) # 리μ€νΈ κ° μΆλ ₯
with open('i_have_a_dream.txt', 'r') as my_file:
i = 0
while True:
line = my_file.readline()
if not line:
break
print(str(i) + '===' + line.replace('\n', '')) # νμ€μ© κ° μΆλ ₯
i += 1
with open("i_have_a_dream.txt", "r") as my_file:
contents = my_file.read()
word_list = contents.split(" ")
#λΉμΉΈ κΈ°μ€μΌλ‘ λ¨μ΄λ₯Ό λΆλ¦¬ 리μ€νΈ
line_list = contents.split("\n")
#νμ€ μ© λΆλ¦¬νμ¬ λ¦¬μ€νΈ
print("Total Number of Characters :", len(contents))
print("Total Number of Words:", len(word_list))
print("Total Number of Lines :", len(line_list))
w
, encoding=utf8
f = open("count_log.txt", 'w', encoding="utf8")
for i in range(1, 11):
data = "%dλ²μ§Έ μ€μ
λλ€.\n" % i
f.write(data)
f.close()
a
λ μΆκ°λͺ¨λwith open("count_log.txt", 'a', encoding="utf8") as f:
for i in range(1, 11):
data = "%dλ²μ§Έ μ€μ
λλ€.\n" % i
f.write(data)
π os λͺ¨λμ μ¬μ©νμ¬ Directory λ€λ£¨κΈ°
import os
os. mkdir('log')
π λλ ν λ¦¬κ° μλμ§ νμΈνκΈ°
if not os.path.isdir('log'):
os.mkdir('log')
import os
if not os.path.isdir('log'):
os.mkdir('log')
if not os.path.exists('log/count_log.txt'):
f = open('log/count_log.txt', 'w', encoding='utf8')
f.write('κΈ°λ‘μ΄ μμλ©λλ€\n')
f.close()
with open('log/count_log.txt', 'a', encoding='utf8') as f:
import random, datetime
for i in range(1, 11):
stamp = str(datetime.datetime.now())
value = random.random() * 1000000
log_line = stamp + '\t' + str(value) +'κ°μ΄ μμ±λμμ΅λλ€' + '\n'
f.write(log_line)
import pickle
f = open('list.pickle', 'wb')
test = [1, 2, 3, 4, 5]
pickle.dump(test, f)
f.close()
f = open(list.pickle', 'rb')
test_pickle = pickle.load(f)
print(test_pickle)
f.close()
import logging
logging.debug("νλ Έμμ!")
logging.info("νμΈν΄")
logging.warning("μ‘°μ¬ν΄!")
logging.error("μλ¬λ¬μ΄!!!")
logging.critical ("λ§νλ€...")
import configparser
config = configparser.ConfigParser()
config.sections()
config.read('example.cfg')
config.sections()
for key in condig['SectionOne']:
print(key)
config['SectionOne']['status']