* ์ด ๊ธ์ ๋ค์ด๋ฒ ๋ถ์คํธ ์ฝ์ค์ ์ธ๊ณต์ง๋ฅ(AI) ๊ธฐ์ด ๋ค์ง๊ธฐ ๊ฐ์๋ฅผ ์๊ฐํ๋ฉฐ ์ ๋ฆฌํ ๊ธ์ ๋๋ค.
File System : OS(์ด์์ฒด์ )์์ ํ์ผ์ ์ ์ฅํ๋ ํธ๋ฆฌ๊ตฌ์กฐ ์ ์ฅ์ฒด๊ณ
์ข ๋ฅ
1. Binary File : ์ปดํจํฐ๊ฐ ์ดํดํ ์ ์๋ ํํ์ธ ์ด์ง(๋ฒ)์ผ๋ก ์ ์ฅ๋ ํ์ผ ex. excel, word ๋ฑ
2. Text File : ์ฌ๋์ด ์ดํดํ ์ ์๋ ๋ฌธ์์ด ํ์์ผ๋ก ์ ์ฅ๋ ํ์ผ๋ก ASCII/Unicode ๋ก ์ ์ฅ๋จ
ex. ๋ฉ๋ชจ์ฅ์ผ๋ก ์ด๋ฆฌ๋ ํ์ผ(HTML, code)
์ฌ์ฉ๋ฐฉ๋ฒ
1) ํ์ผ ์ด๊ธฐ
f = open("ํ์ผ์ด๋ฆ", mode="์ ๊ทผ๋ชจ๋ ์ข ๋ฅ")
cf) ์ ๊ทผ๋ชจ๋ ์ข ๋ฅ
- r (์ฝ๊ธฐ ๋ชจ๋)
- w (์ฐ๊ธฐ ๋ชจ๋)
- a (์ถ๊ฐ ๋ชจ๋) : ํ์ผ์ ๋ง์ง๋ง์ ์๋ก์ด ๋ด์ฉ์ ์ถ๊ฐํ ๋ ์ฌ์ฉ
2) ํ์ผ ๋ซ๊ธฐ
f.close()
3) ํ์ผ ์ฝ๊ธฐ
- ์ ์ฒด ์ฝ๊ธฐ
f = open("ํ์ผ์ด๋ฆ", mode="r")
contents=f.read()
f.close()
or
with open("ํ์ผ์ด๋ฆ", mode="r") as f:
contents=f.read()
with ๊ตฌ๋ฌธ์ด ๋๋๋ฉด ํ์ผ์ด ์๋์ผ๋ก close ๋๋ค.
- ํ์ค์ฉ ์ฝ๊ธฐ
f = open("ํ์ผ์ด๋ฆ", mode="r")
content_list=f.readlines() #ํ์ค์ฉ list ํํ๋ก ์ ์ฅ
f.close()
- ์คํ์๋ง๋ค ํ์ค์ฉ ์ฝ์ด์ค๊ธฐ(์ฉ๋์ด ํด๋)
f = open("ํ์ผ์ด๋ฆ", mode="r")
i=0
while True:
line=f.readline()
if not line: #๋น line์ด ๋์ค๋ฉด ์ข ๋ฃ
break
f.close()
์์ ) ํ์ผ์ด ๋ช๊ฐ์ ๋จ์ด์ ์ค๋ก ๊ตฌ์ฑ๋์ด ์๋์ง ํ์ธํด๋ณด์.
1 2 3 4 5 6 7 | f = open("ํ์ผ์ด๋ฆ", mode="r") contents=f.read() word_list=contents.split(" ") line_list=contents.split("\n") print("๋จ์ด์ : ", len(word_list)) print("์ค์ : ", len(line_list)) f.close() | cs |
4) ํ์ผ ์ฐ๊ธฐ
- ๋ฎ์ด์ฐ๊ธฐ
f =open("ํ์ผ์ด๋ฆ", mode="w", encoding="์ธ์ฝ๋ฉ์ข ๋ฅ")
#ํ์ค์ฉ ์ฐ๊ธฐ
for i in range(10):
f.write("์ฐ๊ณ ์ถ์๋ด์ฉ")
f.close()
or
with open("ํ์ผ์ด๋ฆ", mode="w", encoding="์ธ์ฝ๋ฉ์ข ๋ฅ") as my_file:
#ํ์ค์ฉ ์ฐ๊ธฐ
for i in range(10):
f.write("์ฐ๊ณ ์ถ์๋ด์ฉ")
with ๊ตฌ๋ฌธ์ด ๋๋๋ฉด ํ์ผ์ด ์๋์ผ๋ก close ๋๋ค.
cf) ์ธ์ฝ๋ฉ ์ข ๋ฅ
- utf8 (ํ๊ธ, ๋์์์)
- cp949 (์๋์ฐ)
๋ฐ๋ผ์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ๋๋ ์ธ์ฝ๋ฉ์ ํ์ธํ์.
๊ธฐ๋ณธ์ ์ผ๋ก utf8๋ก ์ ์ฅํ๋๋ก ํ์.
- ์ถ๊ฐ๋ก ์ฐ๊ธฐ
f = open("ํ์ผ์ด๋ฆ", mode="a", encoding="์ธ์ฝ๋ฉ์ข ๋ฅ")
for i in range(5):
f.write("์ฐ๊ณ ์ถ์๋ด์ฉ)
content_list=f.readlines() #ํ์ค์ฉ list ํํ๋ก ์ ์ฅ
f.close()
ํด๋์ ํ์ผ ๋ค๋ฃจ๊ธฐ
-1) ๋๋ ๋๋ฆฌ๋ ํ์ผ์ ๊ฐ์ฒด๋ก ๋ค๋ฃจ๊ธฐ
- ํ์ฌ ๊ฒฝ๋ก
import pathlib
pathlib.Path.cwd()
- ํ์ฌ ๊ฒฝ๋ก์ ์์ ํด๋์ ์ด๋ฆ์ ํ์ธ
import pathlib
pathlib.Path.cwd().parent
- ํ์ฌ ๊ฒฝ๋ก์ ์์ํด๋๋ค์ ์์ฐจ์ ์ผ๋ก ๋ฆฌ์คํธ๋ก ํ์ธ
import pathlib
list(pathlib.Path.cwd().parents)
- ํ์ฌ ๊ฒฝ๋ก์ ๋ชจ๋ ํด๋/ํ์ผ ํ์ธ
import pathlib
list(pathlib.Path.cwd().glob('*'))
0) ํ์ผ ๊ฒฝ๋ก ์ง์ ํ๊ธฐ
import os
os.path.join("๊ฒฝ๋ก", "ํ์ผ๋ช
")
์ด์์ฒด์ ๋ง๋ค ๊ฒฝ๋ก ๊ตฌ๋ถ์๊ฐ ๋ค๋ฅธ๋ฐ, ์ด๋ ์ค๋ฅ๋ฅผ ๋ฐ์์ํค์ง ์๊ธฐ ์ํด์ ์์ ํ์์ ๊ถ์ฅ
1) ํด๋ ๋ง๋ค๊ธฐ
import os
try:
os.mkdir("ํด๋์ด๋ฆ")
except FileExistsError as e:
print("Already created")
cf) ํ์ผ์ด๋ ํด๋๊ฐ ์กด์ฌํ๋์ง ํ์ธํ๋ ์ฝ๋
- os.path.exists("ํด๋์ด๋ฆ")
- os.path.isfile("ํ์ผ๋ช ")
์์ ) ํน์ ํด๋์ ํน์ ํ์ผ์ ๊ธฐ๋กํด๋ณด์.
import os
# ํด๋๊ฐ ์ด๋ฏธ ์กด์ฌํ๋์ง ํ์ธ
if not os.path.exists("ํด๋๋ช
"):
os.mkdir("ํด๋๋ช
")
TARGET_FILE_PATH=os.path.join("ํด๋๋ช
", "ํ์ผ๋ช
")
#ํ์ผ์ด ์ด๋ฏธ ์กด์ฌํ๋์ง ํ์ธ
if not os.path.exists(TARGET_FILE_PATH):
f=open("ํด๋๋ช
/ํ์ผ๋ช
", "a", encoding="utf8")
f.write("๊ธฐ๋ก์ด ์์๋ฉ๋๋ค")
f.close()
'์ธ๊ณต์ง๋ฅ๐พ > ํ์ด์ฌ ๊ธฐ๋ณธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ์ด์ฌ ๊ธฐ๋ณธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ]1. Numpy (0) | 2023.02.05 |
---|---|
[ํ์ด์ฌ ๊ธฐ๋ณธ ๋ฐ์ดํฐ ํ์ ] csv, ์น(web), xml, json (0) | 2023.01.30 |
[ํ์ด์ฌ ๊ธฐ๋ณธ ๊ฐ๋ ] ์์ธ์ฒ๋ฆฌ(Exception Handling) (0) | 2023.01.27 |
[ํ์ด์ฌ ๊ธฐ๋ณธ ๊ฐ๋ ] Conda๋ฅผ ์ด์ฉํ ๊ฐ์ํ๊ฒฝ ํธ๋ค๋ง (0) | 2023.01.26 |
[ํ์ด์ฌ ๊ธฐ๋ณธ ๊ฐ๋ ] ๋ชจ๋๊ณผ ํจํค์ง์ ํ๋ก์ ํธ (0) | 2023.01.24 |