728x90
ํ์ผ ์ด๊ธฐ (Open)
- open() ํจ์๋ฅผ ์ด์ฉํ์ฌ ํ์ผ์ ์ด ์ ์์ต๋๋ค.
- ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ์ด๊ณ ์ถ์ผ๋ฉด 'r', ์ฐ๊ธฐ ์ ์ฉ์ผ๋ก ์ด๊ณ ์ถ์ผ๋ฉด 'w'์ ์ธ์๋ก ์ค๋๋ค.
_file = open(file = "ex1.csv", mode = 'r')
...
# ์์
์ ๋ง์ณค๋ค๋ฉด ๋ซ์์ค๋๋ค.
_file.close()
ํ์ผ ์ฝ๊ธฐ (Read)
- readline() ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ ํ ์ค์ฉ ์ฐจ๋ก๋๋ก ์ฝ์ ์ ์์ต๋๋ค.
_file.readline() # 'a, b, c, d, message\n'
_file.readline() # '1, 2, 3, 4, hello\n'
_file.readline() # '5, 6, 7, 8, world\n'
_file.readline() # '9, 10, 11, 12, foo\n'
_file.readline() # '' -> ๋ง์ง๋ง ๋ผ์ธ๊น์ง ๋ชจ๋ ์ฝ์
- readlines()๋ฅผ ํตํด, ํ ์ค์ฉ ์ฝ์ด ๋ฆฌ์คํธ๋ก ๋ณํํ ์ ์์ต๋๋ค.
- ๋ฆฌ์คํธ๋ก ๋ฐํํ๊ธฐ ๋๋ฌธ์ ํ์ผ ํฌ๊ธฐ๊ฐ ํฌ๋ฉด ๋ฉ๋ชจ๋ฆฌ ๋ถ์กฑ ๋ฌธ์ ๊ฐ ์๊ธธ ์ ์์ต๋๋ค.
for line in _file.readlines():
print(line)
_file.close()
# ์ถ๋ ฅ
# a, b, c, d, message
# 1, 2, 3, 4, hello
# 5, 6, 7, 8, world
# 9, 10, 11, 12, foo
- ๋ฐ๋ผ์, ์ผ๋ฐ์ ์ผ๋ก๋ Lazy loading์ธ ๋ค์ ๋ฐฉ๋ฒ์ ์ฌ์ฉํฉ๋๋ค.
for line in _file:
print(line)
_file.close()
# 'a, b, c, d, message'
# '1, 2, 3, 4, hello'
# '5, 6, 7, 8, world'
# '9, 10, 11, 12, foo'
- read()๋ฅผ ํตํด ๋ชจ๋ ๋ผ์ธ์ ํ ๋ฒ์ ์ฝ์ ์ ์์ต๋๋ค.
print(_file.read())
_file.close()
# a, b, c, d, message
# 1, 2, 3, 4, hello
# 5, 6, 7, 8, world
# 9, 10, 11, 12, foo
ํ์ผ ์ฐ๊ธฐ (Write)
- ํ์ผ์ ์ฐ๊ธฐ ์ ์ฉ(w)์ผ๋ก ์ด์ด์ค๋๋ค.
_file = open(file = "myTest.txt", mode = 'w')
- write() ๋ฉ์๋๋ฅผ ํตํด ํ์ผ์ ์ธ ์ ์์ต๋๋ค. ์ด ๋ ์ฐ๊ธฐ ์์ ์ ์ฒ์๋ถํฐ ๋ค์ ๋ฎ์ด์ฐ๋ ์์ ์ ๋๋ค.
_file.write('''
It was the best of times, it was the worst of times,
it was the age of wisdom, it was the age of foolishness,
it was the epoch of belief, it was the epoch of incredulity,
it was the season of Light, it was the season of Darkness,
it was the spring of hope, it was the winter of despair,
we had everything before us, we had nothing before us,
we were all going direct to Heaven, we were all going direct the other way -
in short, the period was so far like the present period,
that some of its noisiest authorities insisted on its being received,
for good or for evil, in the superlative degree of comparison only.
Tale of Two Cities
by Charles Dickens
''')
_file.close() # ๋ซ์์ฃผ์ง ์์ผ๋ฉด ํ์ผ์ด ์ ์ฅ๋์ง ์์ต๋๋ค.
- ๋ค์ ํ์ผ์ ์ฝ์ด, ์ ์ ์ฅ๋์๋์ง ํ์ธํด๋ด ๋๋ค.
_file = open(file = "myTest.txt", mode = 'r')
print(_file.read())
_file.close()
# ์ถ๋ ฅ
'''
It was the best of times, it was the worst of times,
it was the age of wisdom, it was the age of foolishness,
it was the epoch of belief, it was the epoch of incredulity,
it was the season of Light, it was the season of Darkness,
it was the spring of hope, it was the winter of despair,
we had everything before us, we had nothing before us,
we were all going direct to Heaven, we were all going direct the other way -
in short, the period was so far like the present period,
that some of its noisiest authorities insisted on its being received,
for good or for evil, in the superlative degree of comparison only.
Tale of Two Cities
by Charles Dickens
'''
with
- ํ์ผ ์ฌ์ฉ์ ๋ง์น ๋๋ง๋ค close()๋ฅผ ํด์ฃผ๋ ๊ฒ์ด ๋ฒ๊ฑฐ๋กญ๋ค๋ฉด, with ๊ตฌ๋ฌธ์ ์ฌ์ฉํ๋ฉด ํธ๋ฆฌํฉ๋๋ค.
with open("myTest.txt") as _file:
print(_file.read())
- ์์ with ๊ตฌ๋ฌธ์์๋ with ๋ธ๋ก ๋ด์ ์ฝ๋๊ฐ ์ข ๋ฃ๋จ๊ณผ ๋์์, ํ์ผ์ ์๋์ผ๋ก close() ์ฒ๋ฆฌ ํด์ค๋๋ค.
- ํ์ผ ์ ์ถ๋ ฅ์์๋ with ๊ตฌ๋ฌธ์ ํ์ฉํ๋ ๊ฒ์ด ํธ๋ฆฌํฉ๋๋ค.
# txt ํ์ผ์ ์ฝ๊ณ , ๊ฐ ๋ผ์ธ์ ์์ ๋ผ์ธ ๋ฒํธ ๋งค๊ธฐ๊ธฐ
def AddNums(fileName):
result = []
with open(fileName) as _file:
for number, line in enumerate(_file):
result.append(str(number) + ". " + line)
return result
result = AddNums("myTest.txt")
result
# ๊ฒฐ๊ณผ
'''
['0. \n',
'1. It was the best of times, it was the worst of times,\n',
'2. it was the age of wisdom, it was the age of foolishness,\n',
'3. it was the epoch of belief, it was the epoch of incredulity,\n',
'4. it was the season of Light, it was the season of Darkness,\n',
'5. it was the spring of hope, it was the winter of despair,\n',
'6. we had everything before us, we had nothing before us,\n',
'7. we were all going direct to Heaven, we were all going direct the other way -\n',
'8. in short, the period was so far like the present period,\n',
'9. that some of its noisiest authorities insisted on its being received,\n',
'10. for good or for evil, in the superlative degree of comparison only.\n',
'11. \n',
'12. Tale of Two Cities\n',
'13. by Charles Dickens\n']
'''
728x90
๋ฐ์ํ
'๐ฏ๏ธLanguage > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] Generator, Enumerate, Zip (0) | 2022.06.12 |
---|---|
[Python] Iterable, Iterator (0) | 2022.06.12 |
[Python] Lambda ์ (0) | 2022.06.12 |
[Python] While / else (0) | 2022.06.12 |
[Python] ํ์ด์ฌ์ ํตํด ๊ตฌํํ DFS์ BFS (0) | 2022.05.14 |