10. Brief tour of the standard library¶
10.1. Operating system interface¶
os 모ëì ì´ì ì²´ì ì ìí¸ ìì©í기 ìí ìì ê°ì§ í¨ìë¤ì ì ê³µí©ëë¤:
>>> import os
>>> os.getcwd() # Return the current working directory
'C:\\Python314'
>>> os.chdir('/server/accesslogs') # Change current working directory
>>> os.system('mkdir today') # Run the command mkdir in the system shell
0
from os import * ëì ì import os ì¤íì¼ì ì¬ì©í´ì¼ í©ëë¤. ê·¸ëì¼ os.open() ì´ ë´ì¥ open() ì ê°ë¦¬ë ê²ì í¼í ì ìëë°, ë í¨ìë ì주 ë¤ë¥´ê² ëìí©ëë¤.
os ì ê°ì í° ëª¨ëê³¼ ìì
í ë, ë´ì¥ dir() ê³¼ help() í¨ìë ëíí ëì°ë¯¸ë¡ ì¸ëª¨ê° ììµëë¤.
>>> import os
>>> dir(os)
<returns a list of all module functions>
>>> help(os)
<returns an extensive manual page created from the module's docstrings>
ì¼ìì ì¸ íì¼ê³¼ ëë í°ë¦¬ ê´ë¦¬ ìì
ì ìí´, shutil 모ëì ì¬ì©í기 ì¬ì´ ë ê³ ìì¤ì ì¸í°íì´ì¤ë¥¼ ì ê³µí©ëë¤:
>>> import shutil
>>> shutil.copyfile('data.db', 'archive.db')
'archive.db'
>>> shutil.move('/build/executables', 'installdir')
'installdir'
10.2. File wildcards¶
glob 모ëì ëë í°ë¦¬ ìì¼ëì¹´ë ê²ìì¼ë¡ íì¼ ëª©ë¡ì ë§ëë í¨ì를 ì ê³µí©ëë¤:
>>> import glob
>>> glob.glob('*.py')
['primes.py', 'random.py', 'quote.py']
10.3. Command-line arguments¶
ì¼ë°ì ì¸ ì í¸ë¦¬í° ì¤í¬ë¦½í¸ë ì¢
ì¢
ëª
ë ¹í ì¸ì를 ì²ë¦¬í´ì¼ í íìê° ììµëë¤. ì´ ì¸ìë¤ì sys 모ëì argv ì´í¸ë¦¬ë·°í¸ì 리ì¤í¸ë¡ ì ì¥ë©ëë¤. ì를 ë¤ì´, ë¤ìê³¼ ê°ì demo.py íì¼ì ë³´ê² ìµëë¤:
# íì¼ demo.py
import sys
print(sys.argv)
ë¤ìì ëª
ë ¹íìì python demo.py one two three 를 ì¤íí ì¶ë ¥ì
ëë¤:
['demo.py', 'one', 'two', 'three']
argparse 모ëì ëª
ë ¹ ì¤ ì¸ì를 ì²ë¦¬íë ë ì êµí ë©ì»¤ëì¦ì ì ê³µí©ëë¤. ë¤ì ì¤í¬ë¦½í¸ë íë ì´ìì íì¼ëª
ê³¼ ì íì ì¼ë¡ íìí ì¤ ì를 ì¶ì¶í©ëë¤:
import argparse
parser = argparse.ArgumentParser(
prog='top',
description='Show top lines from each file')
parser.add_argument('filenames', nargs='+')
parser.add_argument('-l', '--lines', type=int, default=10)
args = parser.parse_args()
print(args)
python top.py --lines=5 alpha.txt beta.txt를 ì¬ì©íì¬ ëª
ë ¹ ì¤ìì ì¤íí ë, ì¤í¬ë¦½í¸ë args.lines를 5ë¡, args.filenames를 ['alpha.txt', 'beta.txt']ë¡ ì¤ì í©ëë¤.
10.4. Error output redirection and program termination¶
sys 모ëì stdin, stdout, stderr ì´í¸ë¦¬ë·°í¸ë ê°ê³ ììµëë¤. ê°ì¥ ë§ì§ë§ ê²ì stdout ì´ ë¦¬ëë í¸ ëìì ëë ë³¼ ì ìë ê²½ê³ ì ìë¬ ë©ìì§ë¤ì ì¶ë ¥íëë° ì¸ëª¨ê° ììµëë¤:
>>> sys.stderr.write('Warning, log file not found starting a new one\n')
Warning, log file not found starting a new one
ì¤í¬ë¦½í¸ë¥¼ ì¢
ë£íë ê°ì¥ ì§ì ì ì¸ ë°©ë²ì sys.exit() 를 ì°ë ê²ì
ëë¤.
10.5. String pattern matching¶
re 모ëì ê³ ê¸ ë¬¸ìì´ ì²ë¦¬ë¥¼ ìí ì ê·ì ë구ë¤ì ì ê³µí©ëë¤. ë³µì¡í 매ì¹ê³¼ ì¡°ìì ìí´, ì ê·ìì ê°ê²°íê³ ìµì íë ì루ì
ì ì ê³µí©ëë¤:
>>> import re
>>> re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')
['foot', 'fell', 'fastest']
>>> re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat')
'cat in the hat'
ë¨ì§ ê°ë¨í 기ë¥ë§ íìí ê²½ì°ìë, 문ìì´ ë©ìëë¤ì´ ì í¸ëëë° ì½ê¸° ì½ê³ ëë²ê¹ ì´ ì½ê¸° ë문ì ëë¤:
>>> 'tea for too'.replace('too', 'two')
'tea for two'
10.6. ìí¶
math 모ëì ë¶ë ììì ì°ì°ì ìí íë¶ C ë¼ì´ë¸ë¬ë¦¬ í¨ìë¤ì ëí ì¡ì¸ì¤ë¥¼ ì ê³µí©ëë¤.
>>> import math
>>> math.cos(math.pi / 4)
0.70710678118654757
>>> math.log(1024, 2)
10.0
random 모ëì 무ìì ì íì í ì ìë ë구ë¤ì ì ê³µí©ëë¤:
>>> import random
>>> random.choice(['apple', 'pear', 'banana'])
'apple'
>>> random.sample(range(100), 10) # ëì²´ ìë í본 ì¶ì¶
[30, 83, 16, 4, 8, 81, 41, 50, 18, 33]
>>> random.random() # ë²ì [0.0, 1.0) ë´ììì ììì float
0.17970987693706186
>>> random.randrange(6) # range(6) ìì ì íë ììì ì ì
4
statistics 모ëì ìì¹ ë°ì´í°ì 기본ì ì¸ íµê³ì í¹ì±ë¤ì (íê· , ì¤ê°ê°, ë¶ì°, ë±ë±) ê³ì°í©ëë¤.
>>> import statistics
>>> data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]
>>> statistics.mean(data)
1.6071428571428572
>>> statistics.median(data)
1.25
>>> statistics.variance(data)
1.3720238095238095
SciPy íë¡ì í¸ <https://scipy.org> ë ë¤ë¥¸ ìì¹ ê³ì°ì© 모ëë¤ì ë§ì´ ê°ê³ ììµëë¤.
10.7. Internet access¶
ì¸í°ë·ì ì¡ì¸ì¤íê³ ì¸í°ë· íë¡í ì½ë¤ì ì²ë¦¬íë ë§ì 모ëì´ ììµëë¤. ê°ì¥ ê°ë¨í ë ê°ë URLìì ë°ì´í°ë¥¼ ì½ì´ì¤ë urllib.request ì ë©ì¼ì ë³´ë´ë smtplib ì
ëë¤:
>>> from urllib.request import urlopen
>>> with urlopen('https://docs.python.org/3/') as response:
... for line in response:
... line = line.decode() # Convert bytes to a str
... if 'updated' in line:
... print(line.rstrip()) # Remove trailing newline
...
Last updated on Nov 11, 2025 (20:11 UTC).
>>> import smtplib
>>> server = smtplib.SMTP('localhost')
>>> server.sendmail('soothsayer@example.org', 'jcaesar@example.org',
... """To: jcaesar@example.org
... From: soothsayer@example.org
...
... Beware the Ides of March.
... """)
>>> server.quit()
(ë ë²ì§¸ ìë localhost ìì ë©ì¼ ìë²ê° ì¤íëê³ ìì´ì¼ íë¤ë ê²ì 주ìíì¸ì.)
10.8. Dates and times¶
datetime 모ëì ë ì§ì ìê°ì ì¡°ìíë í´ëì¤ë¤ì ì ê³µíëë°, ê°ë¨í ë°©ë²ê³¼ ë³µì¡í ë°©ë² ëª¨ë ì ê³µí©ëë¤. ë ì§ì ìê° ì°ì ì´ ì§ìëì§ë§, 구íì ì´ì ì ì¶ë ¥ í¬ë§¤í
ê³¼ ì¡°ìì ìí´ í¨ì¨ì ì¼ë¡ 멤ë²ë¥¼ ì¶ì¶íë ë°ì ë§ì¶°ì ¸ ììµëë¤. 모ëì ìê°ë를 ê³ ë ¤íë ê°ì²´ë¤ë ì§ìí©ëë¤.
>>> # dates are easily constructed and formatted
>>> import datetime as dt
>>> now = dt.date.today()
>>> now
datetime.date(2003, 12, 2)
>>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")
'12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.'
>>> # dates support calendar arithmetic
>>> birthday = dt.date(1964, 7, 31)
>>> age = now - birthday
>>> age.days
14368
10.9. Data compression¶
ì¼ë°ì ì¸ ë°ì´í° ë³´ê´ ë° ìì¶ íìë¤ì ë¤ìê³¼ ê°ì 모ëë¤ì´ ì§ì ì§ìí©ëë¤: zlib, gzip, bz2, lzma, zipfile, tarfile.
>>> import zlib
>>> s = b'witch which has which witches wrist watch'
>>> len(s)
41
>>> t = zlib.compress(s)
>>> len(t)
37
>>> zlib.decompress(t)
b'witch which has which witches wrist watch'
>>> zlib.crc32(s)
226805979
10.10. Performance measurement¶
ì¼ë¶ íì´ì¬ ì¬ì©ìë¤ì ê°ì 문ì ì ëí ë¤ë¥¸ ì ê·¼ë²ë¤ì ìëì ì¸ ì±ë¥ì íì íëë° ê¹ì ê´ì¬ì ëê³ ììµëë¤. íì´ì¬ì ì´ë° ì§ë¬¸ë¤ì ì¦ì ëµì 주ë 측ì ë구를 ì ê³µí©ëë¤.
ì를 ë¤ì´, ì¸ìë¤ì ë§êµííë ì íµì ì¸ ë°©ì ëì ì, íí í¨í¹ê³¼ ì¸ í¨í¹ì ì¬ì©íê³ ì íë ì í¹ì ëë ì ììµëë¤. timeit 모ëì ì ë¹í ì±ë¥ ì´ì ì ì ìíê² ë³´ì¬ì¤ëë¤:
>>> from timeit import Timer
>>> Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()
0.57535828626024577
>>> Timer('a,b = b,a', 'a=1; b=2').timeit()
0.54962537085770791
timeit ì ì ë°ëìë ëì¡°ì ì¼ë¡, profile ê³¼ pstats 모ëì í° ë¸ë¡ì ì½ëìì ìê° ìê³ ì¹ì
ì ìë³í기 ìí ë구ë¤ì ì ê³µí©ëë¤.
10.11. Quality control¶
ê³ íì§ì ìíí¸ì¨ì´ë¥¼ ê°ë°íë í ê°ì§ ì ê·¼ë²ì ê°ë°ëë ê° í¨ìì ëí í ì¤í¸ë¥¼ ìì±íê³ , ê·¸ê²ë¤ì ê°ë° íë¡ì¸ì¤ ì¤ì ì주 ì¤ííë ê²ì ëë¤.
doctest 모ëì 모ëì íì´ë³´ê³ íë¡ê·¸ë¨ì ë
ì¤í¸ë§ë¤ì ë´ì¥ë í
ì¤í¸ë¤ì ê²ì¬íë ë구를 ì ê³µí©ëë¤. í
ì¤í¸ ë§ë¤ê¸°ë íë²í í¸ì¶ì ê·¸ ê²°ê³¼ì í¨ê» ë
ì¤í¸ë§ì¼ë¡ ë³µì¬í´ì ë¶ì¬ë£ê¸°ë¥¼ íë ìì¤ì¼ë¡ ê°ë¨í´ì§ëë¤. ì¬ì©ììê² ìì 를 í¨ê» ì ê³µí´ì ì¤ëª
ì를 ê°ì íê³ , doctest 모ëì´ ì¤ëª
ììì ì½ëê° ì¬ì í ì¬ì¤ì¸ì§ íì¸íëë¡ í©ëë¤.
def average(values):
"""ì«ì 목ë¡ì ì°ì íê· ì ê³ì°í©ëë¤.
>>> print(average([20, 30, 70]))
40.0
"""
return sum(values) / len(values)
import doctest
doctest.testmod() # ë´ì¥ë í
ì¤í¸ë¥¼ ìë ê²ì¦í©ëë¤
unittest 모ëì doctest 모ëë§í¼ ì¬ì´ ê²ì ìëì§ë§, ëì± í¬ê´ì ì¸ í
ì¤í¸ ì§í©ì ë³ëì íì¼ë¡ ê´ë¦¬í ì ìê² í©ëë¤:
import unittest
class TestStatisticalFunctions(unittest.TestCase):
def test_average(self):
self.assertEqual(average([20, 30, 70]), 40.0)
self.assertEqual(round(average([1, 5, 7]), 1), 4.3)
with self.assertRaises(ZeroDivisionError):
average([])
with self.assertRaises(TypeError):
average(20, 30, 70)
unittest.main() # ëª
ë ¹íìì í¸ì¶íë©´ 모ë í
ì¤í¸ë¥¼ ìíí©ëë¤
10.12. Batteries included¶
íì´ì¬ì âë°°í°ë¦¬ê° í¬í¨ë©ëë¤â ì² íì ê°ê³ ììµëë¤. ì´ë ë í° í¨í¤ì§ì ì êµíê³ ê°ë ¥í 기ë¥ì íµí´ ê°ì¥ ì ëíë©ëë¤. ì를 ë¤ì´:
xmlrpc.clientìxmlrpc.server모ëì ì격 íë¡ìì í¸ì¶ì 구ííë ì¼ì ê±°ì ì¬ìí ì¼ë¡ ë§ëëë¤. 모ëì ì´ë¦ìë ë¶êµ¬íê³ , XMLì ëí ì§ì ì ì¸ ì§ìì´ë ì²ë¦¬ê° íìíì§ ììµëë¤.The
emailpackage is a library for managing email messages, including MIME and other RFC 5322-based message documents. Unlikesmtplibandpoplibwhich actually send and receive messages, the email package has a complete toolset for building or decoding complex message structures (including attachments) and for implementing internet encoding and header protocols.jsoní¨í¤ì§ë ë리 ì¬ì©ëë ë°ì´í° êµí íìì íì±í기ìí ê°ë ¥í ì§ìì ì ê³µí©ëë¤.csv모ëì ë°ì´í°ë² ì´ì¤ì ì¤íë ëìí¸ìì ì¼ë°ì ì¼ë¡ ì§ìëë ì¼íë¡ êµ¬ë¶ë ê° íìì¼ë¡ íì¼ì ì§ì ì½ê³ ì¸ ì ìëë¡ ì§ìí©ëë¤. XML ì²ë¦¬ëxml.etree.ElementTree,xml.domë°xml.saxí¨í¤ì§ì ìí´ ì§ìë©ëë¤. ì´ë¬í 모ëê³¼ í¨í¤ì§ë¥¼ í¨ê» ì¬ì©íë©´ íì´ì¬ ìì© íë¡ê·¸ë¨ê³¼ ë¤ë¥¸ ë구 ê°ì ë°ì´í° êµíì´ í¬ê² ë¨ìí´ì§ëë¤.sqlite3모ëì SQLite ë°ì´í°ë² ì´ì¤ ë¼ì´ë¸ë¬ë¦¬ì ëí¼ì¸ë°, ì½ê° ë¹íì¤ SQL 구문ì ì¬ì©íì¬ ì ë°ì´í¸ëê³ ì¡ì¸ì¤ ë ì ìë í¼ìì¤í´í¸ ë°ì´í°ë² ì´ì¤ë¥¼ ì ê³µí©ëë¤.êµì íë
gettext,locale, 그리ê³codecsí¨í¤ì§ë¥¼ í¬í¨í ë§ì 모ëì ìí´ ì§ìë©ëë¤.