next up previous contents
Next: ºÒ¿ÏÀüÇÔÀ» ´Ù·ç±â Up: ÀϹÝÀÎÀ» À§ÇÑ ÆÄÀ̽ã Áöħ¼­ Previous: ¹®ÀÚ¿­ÀÇ º¹¼ö   ¸ñ Â÷

ÆÄÀÏ ÀÔÃâ·Â

¿©±â¿¡ °£´ÜÇÑ ÆÄÀÏ ÀÔÃâ·ÂÀÇ ¿¹°¡ Çϳª ÀÖ´Ù:
#Write a file
out_file = open("test.txt","w")
out_file.write("This Text is going to out file\nLook at it and see\n")
out_file.close()

#Read a file
in_file = open("test.txt","r")
text = in_file.read()
in_file.close()

print text,
test.txt ÆÄÀÏÀÇ ³»¿ë°ú Ãâ·ÂÀº ´ÙÀ½°ú °°´Ù:
This Text is going to out file
Look at it and see
¿©·¯ºÐÀÌ ÇÁ·Î±×·¥À» ½ÇÇàÇÑ µð·ºÅ丮¿¡ ±× ÇÁ·Î±×·¥ÀÌ test.txt ¶ó´Â ºÒ¸®¿ì´Â ÆÄÀÏÀ» ÀÛ¼ºÇÏ°í ÀÖÀ½À» ÁÖ¸ñÇ϶ó. ¹®ÀÚ¿­ ¼ÓÀÇ '\n'Àº ÆÄÀ̽㿡°Ô ¹Ù·Î ±× ÁöÁ¡¿¡¼­ »õ·Î¿î ¶óÀÎÀ» Ãâ·ÂÇ϶ó°í Áö½ÃÇÑ´Ù.

ÆÄÀÏ ÀÔÃâ·ÂÀÇ °³¿ä´Â ´ÙÀ½°ú °°´Ù.:

  1. open ÇÔ¼ö·Î ÆÄÀÏ °´Ã¼¸¦ °¡Á®¿Â´Ù.
  2. ±× ÆÄÀÏ °´Ã¼¿¡ ´ëÇÏ¿© (¾î¶² »óÅ·Π¿­·È´À³Ä¿¡ µû¶ó) Àб⠶Ǵ ¾²±â¸¦ ÇÑ´Ù.
  3. ±×°ÍÀ» ´Ý´Â´Ù.

ù° ´Ü°è´Â ÆÄÀÏ°´Ã¼¸¦ ȹµæÇÏ´Â °ÍÀÌ´Ù. ÀÌ°ÍÀ» ÇÏ´Â ¹æ¹ýÀº open ÇÔ¼ö¸¦ »ç¿ëÇÏ´Â °ÍÀÌ´Ù. ±× Çü½ÄÀº file_object = open(filename,mode)À̸ç file_object ´Â ÆÄÀÏ °´Ã¼¸¦ ´ãÀ» º¯¼öÀÌ´Ù, filename È­ÀÏÀ̸§À» ³ªÅ¸³»´Â ¹®ÀÚ¿­À̸ç, mode ´Â "r"·Î Àбâ(read)¸¦, "w" ·Î ¾²±â(w) ¸¦ ³ªÅ¸³½´Ù. ´ÙÀ½À¸·Î ±× ÆÄÀÏ °´Ã¼ÀÇ ÇÔ¼ö´Â È£ÃâµÉ ¼ö ÀÖ´Ù. µÎ°³ÀÇ °¡Àå ÀϹÝÀûÀÎ ÇÔ¼ö´Â read ¿Í write ÀÌ´Ù. write ÇÔ¼ö´Â ¹®ÀÚ¿­À» ÆÄÀÏÀÇ ³¡¿¡´Ù°¡ Ãß°¡ÇÑ´Ù. read ÇÔ¼ö´Â ÆÄÀÏ¿¡¼­ ´ÙÀ½ÀÇ °ÍÀ» Àд´٠±×¸®°í ±×°ÍÀ» ¹®ÀÚ¿­·Î ¹ÝȯÇÑ´Ù. ¸¸ÀÏ ¾Æ¹«·± Àμöµµ ÁÖ¾îÁöÁö ¾Ê´Â´Ù¸é ±×°ÍÀº (ÀÌ ¿¹Á¦¿¡¼­ ½ÇÇàµÈ °Í °°ÀÌ) Àüü È­ÀÏÀ» ¹ÝȯÇÒ °ÍÀÌ´Ù.

ÀÌÁ¦ ¿©±â¿¡ ¿ì¸®°¡ ÀÌÀü¿¡ ¸¸µé¾ú´ø ÀüÈ­ ¹øÈ£ ÇÁ·Î±×·¥ÀÇ »õ·Î¿î ¹öÀüÀÌ ÀÖ´Ù:

import string

def print_numbers(numbers):
    print "Telephone Numbers:"
    for x in numbers.keys():
        print "Name: ",x," \tNumber: ",numbers[x]
    print

def add_number(numbers,name,number):
    numbers[name] = number

def lookup_number(numbers,name):
    if numbers.has_key(name):
        return "The number is "+numbers[name]
    else:
        return name+" was not found"

def remove_number(numbers,name):
    if numbers.has_key(name):
        del numbers[name]
    else:
        print name," was not found"


def load_numbers(numbers,filename):
    in_file = open(filename,"r")
    while 1:
        in_line = in_file.readline()
        if in_line == "":
            break
        in_line = in_line[:-1]
        [name,number] = string.split(in_line,",")
        numbers[name] = number
    in_file.close()

def save_numbers(numbers,filename):
    out_file = open(filename,"w")
    for x in numbers.keys():
        out_file.write(x+","+numbers[x]+"\n")
    out_file.close()


def print_menu():
    print '1. Print Phone Numbers'
    print '2. Add a Phone Number'
    print '3. Remove a Phone Number'
    print '4. Lookup a Phone Number'
    print '5. Load numbers'
    print '6. Save numbers'
    print '7. Quit'
    print
phone_list = {}
menu_choice = 0
print_menu()
while menu_choice != 7:
    menu_choice = input("Type in a number (1-7):")
    if menu_choice == 1:
        print_numbers(phone_list)
    elif menu_choice == 2:
        print "Add Name and Number"
        name = raw_input("Name:")
        phone = raw_input("Number:")
        add_number(phone_list,name,phone)
    elif menu_choice == 3:
        print "Remove Name and Number"
        name = raw_input("Name:")
        remove_number(phone_list,name)
    elif menu_choice == 4:
        print "Lookup Number"
	name = raw_input("Name:")
        print lookup_number(phone_list,name)
    elif menu_choice == 5:
        filename = raw_input("Filename to load:")
        load_numbers(phone_list,filename)
    elif menu_choice == 6:
        filename = raw_input("Filename to save:")
        save_numbers(phone_list,filename)
    elif menu_choice == 7:
        pass
    else:
        print_menu()
print "Goodbye"
ÀÌÁ¦ ±×°ÍÀº ÆÄÀÏÀ» ÀúÀåÇÏ°í ·ÎµåÇÏ´Â °ÍÀ» Æ÷ÇÔÇÑ´Ù. ¿©±â¿¡ ³»°¡ ±×°ÍÀ» µÎ¹ø ½ÇÇàÇÑ Ãâ·ÂÀÌ ÀÖ´Ù:
> python tele2.py
1. Print Phone Numbers
2. Add a Phone Number
3. Remove a Phone Number
4. Lookup a Phone Number
5. Load numbers
6. Save numbers
7. Quit

Type in a number (1-7):2
Add Name and Number
Name:Jill
Number:1234
Type in a number (1-7):2
Add Name and Number
Name:Fred
Number:4321
Type in a number (1-7):1
Telephone Numbers:
Name:  Jill     Number:  1234
Name:  Fred     Number:  4321

Type in a number (1-7):6
Filename to save:numbers.txt
Type in a number (1-7):7
Goodbye
> python tele2.py
1. Print Phone Numbers
2. Add a Phone Number
3. Remove a Phone Number
4. Lookup a Phone Number
5. Load numbers
6. Save numbers
7. Quit

Type in a number (1-7):5
Filename to load:numbers.txt
Type in a number (1-7):1
Telephone Numbers:
Name:  Jill     Number:  1234
Name:  Fred     Number:  4321

Type in a number (1-7):7
Goodbye

ÀÌ ÇÁ·Î±×·¥ÀÇ »õ·Î¿î ºÎºÐÀº ´ÙÀ½°ú °°´Ù:

def load_numbers(numbers,filename):
    in_file = open(filename,"r")
    while 1:
        in_line = in_file.readline()
        if len(in_line) == 0:
            break
        in_line = in_line[:-1]
        [name,number] = string.split(in_line,",")
        numbers[name] = number
    in_file.close()
def save_numbers(numbers,filename):
    out_file = open(filename,"w")
    for x in numbers.keys():
        out_file.write(x+","+numbers[x]+"\n")
    out_file.close()

¸ÕÀú ¿ì¸®´Â ±× ÇÁ·Î±×·¥¿¡¼­ ÀúÀåÇÏ´Â ºÎºÐÀ» »ìÆì º¼ °ÍÀÌ´Ù. ¸ÕÀú ±×°ÍÀº open(filename,"w") ¸í·É¾î·Î ÆÄÀÏ °´Ã¼¸¦ »ý¼ºÇÑ´Ù. ´ÙÀ½À¸·Î ±×°ÍÀº ÀüÈ­¹øÈ£µé °¢°¢À» À§ÇÏ¿© 'out_file.write(x+","+numbers[x]+"\n")' ¸í·É¾î·Î ÇϳªÀÇ ¶óÀÎÀ» »ý¼ºÇØ ³ª°£´Ù. ÀÌ°ÍÀº À̸§, Äĸ¶, ÀüÈ­¹øÈ£¸¦ Æ÷ÇÔÇÏ´Â ÇϳªÀÇ ¶óÀÎÀ» Ãâ·ÂÇÏ°í ´ÙÀ½¿¡ »õ·Î¿î ¶óÀÎÀÌ Ãâ·ÂµÈ´Ù.

ÀоîµéÀÌ´Â ºÎºÐÀº ¾à°£Àº ´õ º¹ÀâÇÏ´Ù. ±×°ÍÀº ÆÄÀÏ °´Ã¼¸¦ ȹµæÇÏ´Â °ÍÀ¸·Î ½ÃÀÛÇÑ´Ù. ±×¸®°í ±×°ÍÀº break¼­¼ú¹®À» ¸¸³¯ ¶§±îÁö ȸµ¹À̸¦ À¯ÁöÇϱâ À§ÇÏ¿© 'while 1:'À» »ç¿ëÇÑ´Ù. ´ÙÀ½À¸·Î in_line = in_file.readline()¶ó´Â ÄÚµå·Î ÇÑ ¶óÀÎÀ» ÀÐ¾î µéÀδÙ. getlineÇÔ¼ö´Â ÆÄÀÏÀÇ ³¡¿¡ ´Ù´Ù¸£¸é ºó ¹®ÀÚ¿­(len(string) == 0)À» ¹ÝȯÇÒ °ÍÀÌ´Ù. if ¼­¼ú¹®Àº ÀÌ°ÍÀ» Á¡°ËÇؼ­ ¸¸ÀÏ ±×·± ÀÏÀÌ ÀϾ´Ù¸é whileȸµ¹À̸¦ ºüÁ®³ª¿Â´Ù. ¹°·Ð readline ÇÔ¼ö°¡ »õ·Î¿î¶óÀÎ ¹®ÀÚ¸¦ ±× ¶óÀÎÀÇ ³¡¿¡¼­ ¹ÝȯÇÏÁö ¾Ê¾Ò´Ù¸é ºó ¹®ÀÚ¿­ÀÌ ºó ¶óÀÎÀÎÁö ȤÀº ÆÄÀÏÀÇ ¸¶Áö¸·ÀÎÁö ¸»ÇØÁÙ ¹æ¹ýÀÌ ¾øÀ¸¹Ç·Î »õ·Î¿î¶óÀÎ ¹®ÀÚ´Â getline ÀÌ ¹ÝȯÇØÁØ °÷¿¡ ³²´Â´Ù.

±×·¯¹Ç·Î ¿ì¸®´Â ±× »õ·Î¿î¶óÀÎ ¹®ÀÚ¸¦ Á¦°ÅÇØ¾ß ÇÑ´Ù. in_line = in_line[:-1] ¶óÀÎÀº ¿ì¸®¸¦ À§ÇÏ¿© ¸¶Áö¸· ¹®ÀÚ¸¦ Á¦°Å ÇÔÀ¸·Î½á ÀÌÀÏÀ» ÇØÁØ´Ù. ´ÙÀ½ÀÇ [name,number] = string.split(in_line,",") ¶óÀÎÀº Äĸ¶À§Ä¡¿¡¼­ À̸§°ú ÀüÈ­¹øÈ£·Î ±× ¶óÀÎÀ» ºÐÇÒÇÑ´Ù. ±×¸®°í³ª¼­ ÀÌ°ÍÀº numbers »çÀü¿¡ µî·ÏµÈ´Ù.


next up previous contents
Next: ºÒ¿ÏÀüÇÔÀ» ´Ù·ç±â Up: ÀϹÝÀÎÀ» À§ÇÑ ÆÄÀ̽ã Áöħ¼­ Previous: ¹®ÀÚ¿­ÀÇ º¹¼ö   ¸ñ Â÷
Josh Cogliati jjc@iname.com