Programming for everybody (Python) - 7.Files
7. Files
그동안 우리가 한 건 그냥 정말 python이랑 논 거. 그냥 CPU랑 Main Memory사이를 왔다갔다 하면서. 절대 여길 떠나지 않았지! 그래서 이젠 Secondary Memory를 사용해보려고 해! permanent media! 우리는 python을 이용해서 파일을 쓰거나 읽을 수 있지!
File Processing
텍스트 파일은 sequence of lines라고 볼 수 있지
Opening a File
일단 open()
function을 사용해서 Python에게 우리가 어떤 파일을 읽을건지 말해줘야 한다. open()
은 file handle 을 return해! 뭐 그냥 file 관련 동작을 수행할 수 있는 variable?
어떻게 보면 보통의 프로그램에서 수행하는 [File > Open] 동작이랑 비슷하다고 보면 됨!
Using open()
built-in function in Python1
handle = open(filename, mode)
- return a handle use to manipulate the file
- filename is a string
- mode is optional and should be ‘r’ if we are planning reading the file and ‘w’ if we are going to write to the file
What is a Handle?
1 | fhand = open('mbox.txt') |
When Files are Missing
Traceback을 보게 되겠지. IOError NO such file or directory!
The newline Character
special charater in files. 라인의 끝을 가리킴 => '\n'
=> 얘는 여전히 one charater다!1
2
3
4
5
6'Hello\nWorld!' stuff =
stuff
'Hello\nWorld!'
print stuff
Hello
World!
File Processing
A text file can be thought of as a sequence of lines
각각의 라인 끝에는 \n
character가 있겠지!
File Handle as a Sequence
파일은 sequence of lines라고 했지. sequence는 for로 interative하게 사용할 수 있어. sequence는 ordered set이라는 걸 기억해라!1
2
3xfile = open('mbox.txt')
for cheese in xfile:
print cheese
Counting Lines in a File
1 | fhand = open('mbox.txt') |
Reading the Whole File
큰 파일은 당연히 힘들겠지만, 작은 건 괜춘!1
2
3fhand = open('mbox-short.txt')
inp = fhand.read()
print len(inp)
전체 파일의 character 수를 알려줌
Searching Through a File
1 | fhand = open('mbox-short.txt') |
BUT! 이렇게 하면 문제가 생겨! 아마 중간에 빈 줄이 하나씩 나올거야! 왜일까?
원래 문장에는 \n
(newline character)가 있었겠지. 그리고 print는 newline character를 각각의 line에 더한다. 그러니 그게 중복이 돼서 빈 줄이 하나씩 나오는 것.
Searching Through a File (fixed)
그렇다면 문자열에서 오른쪽에 붙는 whitespace를 잘라버리면 간단하다. rstrip()
1
2
3
4
5fhand = open('mbox-short.txt')
for line in fhand:
line = line.rstrip()
if line.startswith('Form:'):
print line
Skipping with continue
1 | fhand = open('mbox-short.txt') |
Using in to select lines
1 | fhand = open('mbox-short.txt') |
Prompt for File Name
1 | fname = raw_input('Enter the file name: ') |
Bad File Names
try
& except
구문을 이용해 이상한 파일명이 들어와서 프로그램이 멈추는 걸 방지하자!1
2
3
4
5
6
7
8
9
10
11frame = raw_input('Enter the file name:')
try:
fhand = open(fname)
except:
print 'File cannot be opened:', fname
exit()
count = 0
for line in fhand:
if line.startswith('Subject:'):
count = count + 1
print 'There were ', count, 'subject lines in', frame