KR_InputOutput - somaz94/python-study GitHub Wiki
ํ์ด์ฌ์์๋ input()
ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์๋ก๋ถํฐ ์
๋ ฅ์ ๋ฐ์ ์ ์๋ค.
# ๊ธฐ๋ณธ ์
๋ ฅ
name = input()
# ํ๋กฌํํธ์ ํจ๊ป ์
๋ ฅ ๋ฐ๊ธฐ
age = input("๋์ด๋ฅผ ์
๋ ฅํ์ธ์: ")
# ์ซ์ ์
๋ ฅ ๋ฐ๊ธฐ (๋ฌธ์์ด์ ์ซ์๋ก ๋ณํ)
number = int(input("์ซ์๋ฅผ ์
๋ ฅํ์ธ์: "))
float_number = float(input("์ค์๋ฅผ ์
๋ ฅํ์ธ์: "))
# ์ฌ๋ฌ ๊ฐ ํ๋ฒ์ ์
๋ ฅ ๋ฐ๊ธฐ (๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ)
x, y = map(int, input("๋ ์ ์๋ฅผ ์
๋ ฅํ์ธ์: ").split())
print(f"์
๋ ฅํ ๋ ์์ ํฉ: {x + y}")
# ์ฌ๋ฌ ๊ฐ ํ๋ฒ์ ์
๋ ฅ ๋ฐ๊ธฐ (์ผํ๋ก ๊ตฌ๋ถ)
a, b, c = map(float, input("์ธ ์ค์๋ฅผ ์ผํ๋ก ๊ตฌ๋ถํ์ฌ ์
๋ ฅํ์ธ์: ").split(','))
print(f"ํ๊ท : {(a + b + c) / 3}")
# ๋ฆฌ์คํธ๋ก ์ฌ๋ฌ ๊ฐ ์
๋ ฅ ๋ฐ๊ธฐ
numbers = list(map(int, input("์ฌ๋ฌ ์ซ์๋ฅผ ์
๋ ฅํ์ธ์: ").split()))
print(f"์
๋ ฅํ ์ซ์๋ค: {numbers}")
print(f"ํฉ๊ณ: {sum(numbers)}")
-
input()
์ ํญ์ ๋ฌธ์์ด(string)์ ๋ฐํํ๋ค - ์ซ์๋ฅผ ์
๋ ฅ๋ฐ์ ๋๋
int()
๋๋float()
๋ก ๋ณํ์ด ํ์ํ๋ค - ์๋ชป๋ ํ์ ์ ๋ ฅ ์ ์์ธ๊ฐ ๋ฐ์ํ ์ ์์ผ๋ฏ๋ก ์์ธ ์ฒ๋ฆฌ๊ฐ ๊ถ์ฅ๋๋ค
# ์ซ์ ์
๋ ฅ ๊ฒ์ฆ
while True:
try:
age = int(input("๋์ด๋ฅผ ์
๋ ฅํ์ธ์: "))
if age < 0 or age > 150:
print("์ ํจํ ๋์ด ๋ฒ์๊ฐ ์๋๋๋ค. ๋ค์ ์
๋ ฅํ์ธ์.")
continue
break
except ValueError:
print("์ซ์๋ง ์
๋ ฅํ์ธ์.")
print(f"์
๋ ฅํ ๋์ด: {age}")
# ์ ํ์ง ์
๋ ฅ ๊ฒ์ฆ
valid_options = ['A', 'B', 'C']
while True:
choice = input(f"์ต์
์ ์ ํํ์ธ์ ({'/'.join(valid_options)}): ").upper()
if choice in valid_options:
break
print("์ ํจํ์ง ์์ ์ต์
์
๋๋ค. ๋ค์ ์
๋ ฅํ์ธ์.")
print(f"์ ํํ ์ต์
: {choice}")
# ๋จ์ ์ถ๋ ฅ
print("Hello, World!")
# ๋ณ์ ์ถ๋ ฅ
name = "Alice"
age = 25
print(name, age)
# ๊ตฌ๋ถ์(sep) ์ง์
print("Hello", "World", sep="-") # Hello-World
# ๋๋ฌธ์(end) ์ง์
print("Hello", end="! ") # Hello!
print("World") # World
# ์ฌ๋ฌ ์ค ์ถ๋ ฅ
print("""
์ฒซ ๋ฒ์งธ ์ค
๋ ๋ฒ์งธ ์ค
์ธ ๋ฒ์งธ ์ค
""")
# ๋ณ์์ ํ
์คํธ ํจ๊ป ์ถ๋ ฅ
print("์ด๋ฆ:", name, "๋์ด:", age)
name = "Alice"
age = 25
height = 165.5
# 1. % ์ฐ์ฐ์ ์ฌ์ฉ (์ค๋๋ ๋ฐฉ์)
print("์ด๋ฆ: %s, ๋์ด: %d, ํค: %.1f" % (name, age, height))
# 2. format() ๋ฉ์๋ ์ฌ์ฉ
print("์ด๋ฆ: {}, ๋์ด: {}, ํค: {:.1f}".format(name, age, height))
# ์ธ๋ฑ์ค ์ง์
print("์ด๋ฆ: {0}, ๋์ด: {1}, ํค: {2:.1f}".format(name, age, height))
print("๋์ด: {1}, ์ด๋ฆ: {0}, ํค: {2:.1f}".format(name, age, height))
# ์ด๋ฆ ์ง์
print("์ด๋ฆ: {n}, ๋์ด: {a}, ํค: {h:.1f}".format(n=name, a=age, h=height))
# 3. f-string (Python 3.6+, ๊ฐ์ฅ ๊ถ์ฅ๋จ)
print(f"์ด๋ฆ: {name}, ๋์ด: {age}, ํค: {height:.1f}")
# f-string ๋ด ํํ์ ์ฌ์ฉ
print(f"๋ด๋
๋์ด: {age + 1}")
print(f"์ธ์น ๋จ์ ํค: {height / 2.54:.2f}inch")
# f-string ๋ด ๋์
๋๋ฆฌ ์ฌ์ฉ
person = {'name': 'Bob', 'age': 30}
print(f"์ด๋ฆ: {person['name']}, ๋์ด: {person['age']}")
# ์ ๋ ฌ ๋ฐ ์ฑ์ ๋ฌธ์
print(f"{'์ผ์ชฝ ์ ๋ ฌ':<15}|") # ์ผ์ชฝ ์ ๋ ฌ |
print(f"{'์ค๋ฅธ์ชฝ ์ ๋ ฌ':>15}|") # ์ค๋ฅธ์ชฝ ์ ๋ ฌ|
print(f"{'๊ฐ์ด๋ฐ ์ ๋ ฌ':^15}|") # ๊ฐ์ด๋ฐ ์ ๋ ฌ |
print(f"{'0์ผ๋ก ์ฑ์':0>15}|") # 00000000000์ผ๋ก ์ฑ์|
# ์ฒ ๋จ์ ๊ตฌ๋ถ๊ธฐํธ
amount = 1234567890
print(f"๊ธ์ก: {amount:,}") # ๊ธ์ก: 1,234,567,890
# 10์ง์ (๊ธฐ๋ณธ)
print(f"{42}") # 42
# 2์ง์
print(f"{42:b}") # 101010
# 8์ง์
print(f"{42:o}") # 52
# 16์ง์ (์๋ฌธ์)
print(f"{42:x}") # 2a
# 16์ง์ (๋๋ฌธ์)
print(f"{42:X}") # 2A
# ์ง์ ํ๊ธฐ๋ฒ
print(f"{1234567:e}") # 1.234567e+06
# ๋ฐฑ๋ถ์จ
print(f"{0.25:%}") # 25.000000%
print(f"{0.25:.0%}") # 25%
# ์์์ ์๋ฆฟ์ ์ง์
pi = 3.14159265359
print(f"{pi:.2f}") # 3.14
print(f"{pi:.4f}") # 3.1416
# ์ถ๋ ฅ ๋๋น ์ง์
for i in range(1, 11):
print(f"{i:2d} {i*i:3d} {i*i*i:4d}")
# ํ์ผ ์ฐ๊ธฐ (write mode)
with open('example.txt', 'w', encoding='utf-8') as f:
f.write('์ฒซ ๋ฒ์งธ ์ค\n')
f.write('๋ ๋ฒ์งธ ์ค\n')
# ์ฌ๋ฌ ์ค ํ๋ฒ์ ์ฐ๊ธฐ
lines = ['์ธ ๋ฒ์งธ ์ค\n', '๋ค ๋ฒ์งธ ์ค\n', '๋ค์ฏ ๋ฒ์งธ ์ค\n']
f.writelines(lines)
# ํ์ผ ์ฝ๊ธฐ (read mode)
with open('example.txt', 'r', encoding='utf-8') as f:
# ํ์ผ ์ ์ฒด ์ฝ๊ธฐ
content = f.read()
print(content)
# ํ ์ค์ฉ ์ฝ๊ธฐ
with open('example.txt', 'r', encoding='utf-8') as f:
first_line = f.readline() # ํ ์ค ์ฝ๊ธฐ
print(first_line, end='') # ์ด๋ฏธ \n์ด ํฌํจ๋์ด ์์
# ๋ชจ๋ ์ค ์ฝ๊ธฐ
for line in f: # ํ์ผ ๊ฐ์ฒด๋ ์ดํฐ๋ฌ๋ธ
print(line, end='')
# ๋ชจ๋ ์ค์ ๋ฆฌ์คํธ๋ก ์ฝ๊ธฐ
with open('example.txt', 'r', encoding='utf-8') as f:
lines = f.readlines() # ์ค ๋จ์๋ก ๋ฆฌ์คํธ ๋ฐํ
print(lines) # ['์ฒซ ๋ฒ์งธ ์ค\n', '๋ ๋ฒ์งธ ์ค\n', ...]
# ํ์ผ์ ๋ด์ฉ ์ถ๊ฐ (append mode)
with open('example.txt', 'a', encoding='utf-8') as f:
f.write('์ถ๊ฐ๋ ์ค\n')
# 'w': ์ฐ๊ธฐ ๋ชจ๋ (ํ์ผ์ด ์์ผ๋ฉด ๋ด์ฉ ์ง์ฐ๊ณ ์๋ก ์์ฑ)
with open('file.txt', 'w', encoding='utf-8') as f:
f.write('์๋ก์ด ๋ด์ฉ\n')
# 'r': ์ฝ๊ธฐ ๋ชจ๋ (๊ธฐ๋ณธ๊ฐ)
with open('file.txt', 'r', encoding='utf-8') as f:
content = f.read()
# 'a': ์ถ๊ฐ ๋ชจ๋ (ํ์ผ ๋์ ๋ด์ฉ ์ถ๊ฐ)
with open('file.txt', 'a', encoding='utf-8') as f:
f.write('์ถ๊ฐ ๋ด์ฉ\n')
# 'x': ๋ฐฐํ์ ์์ฑ ๋ชจ๋ (ํ์ผ์ด ์ด๋ฏธ ์์ผ๋ฉด ์คํจ)
try:
with open('new_file.txt', 'x', encoding='utf-8') as f:
f.write('์ ํ์ผ ๋ด์ฉ\n')
except FileExistsError:
print('ํ์ผ์ด ์ด๋ฏธ ์กด์ฌํฉ๋๋ค.')
# 'b': ๋ฐ์ด๋๋ฆฌ ๋ชจ๋ (ํ
์คํธ ๋ชจ๋์ ํจ๊ป ์ฌ์ฉ)
with open('binary.dat', 'wb') as f: # ๋ฐ์ด๋๋ฆฌ ์ฐ๊ธฐ
f.write(b'Binary data')
with open('binary.dat', 'rb') as f: # ๋ฐ์ด๋๋ฆฌ ์ฝ๊ธฐ
binary_data = f.read()
# '+': ์ฝ๊ธฐ/์ฐ๊ธฐ ๋ชจ๋ (๋ค๋ฅธ ๋ชจ๋์ ํจ๊ป ์ฌ์ฉ)
with open('file.txt', 'r+', encoding='utf-8') as f: # ์ฝ๊ธฐ+์ฐ๊ธฐ
content = f.read()
f.write('ํ์ผ ๋์ ์ถ๊ฐ\n')
with open('file.txt', 'w+', encoding='utf-8') as f: # ์ฐ๊ธฐ+์ฝ๊ธฐ
f.write('์ ๋ด์ฉ\n')
f.seek(0) # ํ์ผ ํฌ์ธํฐ๋ฅผ ์ฒ์์ผ๋ก ์ด๋
content = f.read()
with open('example.txt', 'r+', encoding='utf-8') as f:
# ํ์ฌ ์์น ํ์ธ
pos = f.tell()
print(f"ํ์ฌ ์์น: {pos}")
# ์์น ์ด๋
f.seek(10) # 10๋ฒ์งธ ๋ฐ์ดํธ๋ก ์ด๋
print(f"10๋ฒ์งธ ์์น์ ๋ฌธ์: {f.read(1)}")
# ์ฒ์์ผ๋ก ์ด๋
f.seek(0)
print(f"์ฒ์์ผ๋ก ์ด๋ ํ ์ฝ๊ธฐ: {f.read(5)}")
# ์๋์ ์ด๋
f.seek(0) # ๋จผ์ ์ฒ์์ผ๋ก ์ด๋
f.read(5) # 5๊ธ์ ์ฝ๊ธฐ
f.seek(10, 1) # ํ์ฌ ์์น์์ 10๋ฐ์ดํธ ์์ผ๋ก
print(f"์๋ ์ด๋ ํ ์ฝ๊ธฐ: {f.read(5)}")
# ํ์ผ ๋์ผ๋ก ์ด๋
f.seek(0, 2) # 2๋ ํ์ผ ๋์ ์๋ฏธ
print(f"ํ์ผ ๋ ์์น: {f.tell()}")
import csv
# CSV ํ์ผ ์ฐ๊ธฐ
data = [
['์ด๋ฆ', '๋์ด', '์ง์
'],
['๊น์ฒ ์', 28, '๊ฐ๋ฐ์'],
['์ด์ํฌ', 32, '๋์์ด๋'],
['๋ฐ์ง๋ฏผ', 24, 'ํ์']
]
with open('data.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerows(data) # ์ฌ๋ฌ ํ ํ๋ฒ์ ์ฐ๊ธฐ
# ๋๋ ํ๋ณ๋ก ์ฐ๊ธฐ
# for row in data:
# writer.writerow(row)
# CSV ํ์ผ ์ฝ๊ธฐ
with open('data.csv', 'r', encoding='utf-8') as f:
reader = csv.reader(f)
for row in reader:
print(row)
# ๋์
๋๋ฆฌ๋ก CSV ์ฒ๋ฆฌ
dict_data = [
{'์ด๋ฆ': '๊น์ฒ ์', '๋์ด': 28, '์ง์
': '๊ฐ๋ฐ์'},
{'์ด๋ฆ': '์ด์ํฌ', '๋์ด': 32, '์ง์
': '๋์์ด๋'},
{'์ด๋ฆ': '๋ฐ์ง๋ฏผ', '๋์ด': 24, '์ง์
': 'ํ์'}
]
with open('dict_data.csv', 'w', newline='', encoding='utf-8') as f:
fieldnames = ['์ด๋ฆ', '๋์ด', '์ง์
']
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader() # ํค๋ ์ฐ๊ธฐ
writer.writerows(dict_data) # ๋ฐ์ดํฐ ์ฐ๊ธฐ
# ๋์
๋๋ฆฌ๋ก CSV ์ฝ๊ธฐ
with open('dict_data.csv', 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
for row in reader:
print(f"{row['์ด๋ฆ']}๋ {row['๋์ด']}์ธ {row['์ง์
']}์
๋๋ค.")
import json
# JSON ํ์ผ ์ฐ๊ธฐ
data = {
'name': 'Kim',
'age': 25,
'skills': ['Python', 'JavaScript'],
'active': True,
'details': {
'email': '[email protected]',
'phone': '010-1234-5678'
}
}
with open('data.json', 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2) # ํ๊ธ ๊ทธ๋๋ก ์ ์ฅ, ๋ค์ฌ์ฐ๊ธฐ ์ ์ฉ
# JSON ํ์ผ ์ฝ๊ธฐ
with open('data.json', 'r', encoding='utf-8') as f:
loaded_data = json.load(f)
print(loaded_data['name'])
print(loaded_data['skills'][0])
print(loaded_data['details']['email'])
# JSON ๋ฌธ์์ด ๋ณํ
json_str = json.dumps(data, ensure_ascii=False)
print(json_str)
# JSON ๋ฌธ์์ด ํ์ฑ
parsed_data = json.loads(json_str)
print(parsed_data['age'])
# ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ ์ฐ๊ธฐ
with open('binary.dat', 'wb') as f:
# ๋ฐ์ดํธ ๋ฐ์ดํฐ ์ฐ๊ธฐ
f.write(b'Hello, binary world!')
# ์ ์ ๊ฐ์ ๋ฐ์ดํธ๋ก ๋ณํํ์ฌ ์ฐ๊ธฐ
import struct
numbers = [1, 2, 3, 4, 5]
for num in numbers:
f.write(struct.pack('i', num)) # 'i'๋ 4๋ฐ์ดํธ ์ ์ ํ์
# ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ ์ฝ๊ธฐ
with open('binary.dat', 'rb') as f:
# ์ฒ์ 21๋ฐ์ดํธ ์ฝ๊ธฐ (๋ฌธ์์ด)
text = f.read(21)
print(text) # b'Hello, binary world!'
# 4๋ฐ์ดํธ์ฉ ์ฝ์ด์ ์ ์๋ก ๋ณํ
import struct
numbers = []
while True:
data = f.read(4) # 4๋ฐ์ดํธ ์ฝ๊ธฐ
if not data: # ํ์ผ ๋์ด๋ฉด ์ข
๋ฃ
break
number = struct.unpack('i', data)[0]
numbers.append(number)
print(numbers) # [1, 2, 3, 4, 5]
import os
import shutil
# ํ์ผ ์กด์ฌ ์ฌ๋ถ ํ์ธ
if os.path.exists('example.txt'):
print('ํ์ผ์ด ์กด์ฌํฉ๋๋ค.')
# ํ์ผ์ธ์ง ๋๋ ํ ๋ฆฌ์ธ์ง ํ์ธ
if os.path.isfile('example.txt'):
print('ํ์ผ์
๋๋ค.')
elif os.path.isdir('example_dir'):
print('๋๋ ํ ๋ฆฌ์
๋๋ค.')
# ํ์ผ ํฌ๊ธฐ ํ์ธ
if os.path.exists('example.txt'):
size = os.path.getsize('example.txt')
print(f'ํ์ผ ํฌ๊ธฐ: {size} ๋ฐ์ดํธ')
# ํ์ผ ์ด๋ฆ ๋ณ๊ฒฝ
if os.path.exists('example.txt'):
os.rename('example.txt', 'new_name.txt')
# ํ์ผ ๋ณต์ฌ
if os.path.exists('new_name.txt'):
shutil.copy('new_name.txt', 'copy.txt')
# ํ์ผ ์ด๋
if os.path.exists('copy.txt'):
if not os.path.exists('temp'):
os.mkdir('temp') # ๋๋ ํ ๋ฆฌ ์์ฑ
shutil.move('copy.txt', 'temp/moved.txt')
# ํ์ผ ์ญ์
if os.path.exists('new_name.txt'):
os.remove('new_name.txt')
# ๋๋ ํ ๋ฆฌ ์์ฑ
if not os.path.exists('new_dir'):
os.mkdir('new_dir') # ๋จ์ผ ๋๋ ํ ๋ฆฌ ์์ฑ
# os.makedirs('path/to/nested/dir') # ์ค์ฒฉ ๋๋ ํ ๋ฆฌ ์์ฑ
# ๋๋ ํ ๋ฆฌ ์ญ์
if os.path.exists('new_dir'):
os.rmdir('new_dir') # ๋น ๋๋ ํ ๋ฆฌ๋ง ์ญ์ ๊ฐ๋ฅ
# shutil.rmtree('dir_with_files') # ํ์ผ์ด ์๋ ๋๋ ํ ๋ฆฌ ์ญ์
# ํ์ฌ ์์
๋๋ ํ ๋ฆฌ
current_dir = os.getcwd()
print(f'ํ์ฌ ๋๋ ํ ๋ฆฌ: {current_dir}')
# ๋๋ ํ ๋ฆฌ ๋ด์ฉ ๋์ด
if os.path.exists('.'):
files = os.listdir('.')
print('ํ์ฌ ๋๋ ํ ๋ฆฌ ๋ด์ฉ:')
for file in files:
print(f'- {file}')
ํ์ด์ฌ์์๋ ์ธ ๊ฐ์ง ๊ธฐ๋ณธ ์คํธ๋ฆผ์ ์ ๊ณตํ๋ค.
import sys
# ํ์ค ์ถ๋ ฅ (Standard Output)
sys.stdout.write("ํ์ค ์ถ๋ ฅ์ ์ง์ ์ฐ๊ธฐ\n")
print("print ํจ์๋ sys.stdout์ ์ถ๋ ฅํฉ๋๋ค.")
# ํ์ค ์ค๋ฅ (Standard Error)
sys.stderr.write("์ค๋ฅ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํฉ๋๋ค.\n")
# ํ์ค ์
๋ ฅ (Standard Input)
print("์ด๋ฆ์ ์
๋ ฅํ์ธ์:")
name = sys.stdin.readline().strip() # input() ํจ์์ ๊ธฐ๋ณธ ๋์
print(f"์
๋ ฅํ ์ด๋ฆ: {name}")
# ์ถ๋ ฅ ๋ฆฌ๋ค์ด๋ ์
์์
original_stdout = sys.stdout # ์๋ stdout ์ ์ฅ
# ํ์ผ๋ก ์ถ๋ ฅ ๋ฆฌ๋ค์ด๋ ์
with open('output.txt', 'w', encoding='utf-8') as f:
sys.stdout = f # stdout์ ํ์ผ๋ก ๋ณ๊ฒฝ
print("์ด ๋ด์ฉ์ ํ์ผ์ ๊ธฐ๋ก๋ฉ๋๋ค.")
print("์ด ๋ด์ฉ๋ ํ์ผ์ ๊ธฐ๋ก๋ฉ๋๋ค.")
sys.stdout = original_stdout # stdout ๋ณต์
print("๋ค์ ์ฝ์์ ์ถ๋ ฅ๋ฉ๋๋ค.")
import pickle
# ๋ณต์กํ ๊ฐ์ฒด ์์ฑ
data = {
'name': 'Alice',
'age': 30,
'grades': [85, 90, 92],
'details': {
'address': '์์ธ์ ๊ฐ๋จ๊ตฌ',
'phone': '010-1234-5678'
}
}
# ๊ฐ์ฒด ์ง๋ ฌํ (์ ์ฅ)
with open('data.pkl', 'wb') as f:
pickle.dump(data, f)
# ๊ฐ์ฒด ์ญ์ง๋ ฌํ (๋ก๋)
with open('data.pkl', 'rb') as f:
loaded_data = pickle.load(f)
print(loaded_data)
print(f"์ด๋ฆ: {loaded_data['name']}")
print(f"์์ธ ์ ๋ณด: {loaded_data['details']}")
# ์ฌ๋ฌ ๊ฐ์ฒด ์ง๋ ฌํ
objects = [
[1, 2, 3, 4],
{'a': 1, 'b': 2},
'Hello, Pickle',
(5, 6, 7)
]
with open('multiple.pkl', 'wb') as f:
for obj in objects:
pickle.dump(obj, f)
# ์ฌ๋ฌ ๊ฐ์ฒด ์ญ์ง๋ ฌํ
with open('multiple.pkl', 'rb') as f:
try:
while True:
obj = pickle.load(f)
print(obj)
except EOFError:
# ํ์ผ ๋์ ๋๋ฌํ๋ฉด EOFError ๋ฐ์
pass
from io import StringIO, BytesIO
# ๋ฌธ์์ด ๊ธฐ๋ฐ ํ์ผ ๊ฐ์ฒด
output = StringIO()
output.write('First line.\n')
output.write('Second line.\n')
# ํ์ฌ ๊ฐ ์ป๊ธฐ
contents = output.getvalue()
print(contents)
# ์ฒ์์ผ๋ก ์์น ์ด๋
output.seek(0)
# ์ฝ๊ธฐ
print(output.read())
# ๋ซ๊ธฐ
output.close()
# ์ด๊ธฐ ๋ด์ฉ์ผ๋ก ์์ฑ
input = StringIO('Initial value.\nAnother line.\n')
print(input.read())
input.close()
# ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ์ฉ BytesIO
binary_output = BytesIO()
binary_output.write(b'Binary data\n')
binary_output.write(b'More binary data\n')
# ์ฒ์์ผ๋ก ์ด๋
binary_output.seek(0)
print(binary_output.read())
binary_output.close()
โ ์ ์ถ๋ ฅ ๋ชจ๋ฒ ์ฌ๋ก:
- ํ์ผ ์์
์๋ ํญ์
with
๋ฌธ ์ฌ์ฉ (์๋์ผ๋ก ํ์ผ์ ๋ซ์์ค) - ํ ์คํธ ํ์ผ ์ฝ๊ธฐ/์ฐ๊ธฐ ์ ์ธ์ฝ๋ฉ ๋ช ์ (๋ณดํต 'utf-8')
- ์ฌ์ฉ์ ์ ๋ ฅ์ ํญ์ ๊ฒ์ฆํ๊ณ ์ ์ ํ ํ๋ณํ
- ํ์ผ ๊ฒฝ๋ก๋
os.path.join()
์ผ๋ก ์์ฑ (์ด์์ฒด์ ๊ฐ ํธํ์ฑ) - JSON ์ฒ๋ฆฌ ์
ensure_ascii=False
์ต์ ์ผ๋ก ํ๊ธ ๋ฑ ์ ๋์ฝ๋ ๋ฌธ์ ์ ์ง - CSV ํ์ผ ์ฐ๊ธฐ ์
newline=''
์ค์ (์ค๋ฐ๊ฟ ๋ฌธ์ ๋ฐฉ์ง) - ๋์ฉ๋ ํ์ผ์ ํ ๋ฒ์ ๋ชจ๋ ์ฝ์ง ๋ง๊ณ ํ ์ค์ฉ ๋๋ ์ฒญํฌ ๋จ์๋ก ์ฒ๋ฆฌ
- ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ๋ ํญ์ ๋ฐ์ด๋๋ฆฌ ๋ชจ๋('rb', 'wb')๋ก ์ฒ๋ฆฌ
- ์์ธ ์ฒ๋ฆฌ๋ฅผ ํตํด ์ ์ถ๋ ฅ ์ค๋ฅ์ ๋์
- ๋ฌธ์์ด ํฌ๋งทํ ์ f-string ์ฌ์ฉ ๊ถ์ฅ (Python 3.6+)