34
<write>
file = open("/Users/song-yeongseog/Desktop/PYTHON/textfile/test.txt", "w")
strCount = file.write("Hello python!")
print(f"strCount: {strCount}")
file.close()
<실습>
import time
lt = time.localtime()
dateStr = "[" + str(lt.tm_year) + "년" + str(lt.tm_mon) + "월" + str(lt.tm_mday) + "일]"
todaySchedule = input("오늘 일정: ")
file = open("/Users/song-yeongseog/Desktop/PYTHON/textfile/test.txt", "w")
file.write(dateStr + todaySchedule)
file.close()
35
<read>
file = open("/Users/song-yeongseog/Desktop/PYTHON/textfile/test.txt", "r")
str = file.read()
print(f"str: {str}")
file.close()
<실습>
file = open("/Users/song-yeongseog/Desktop/PYTHON/textfile/about_python.txt", "r")
str = file.read()
print(f"str: {str}")
file.close()
str = str.replace("Python", "파이썬", 2)
print(f"str: {str}")
file = open("/Users/song-yeongseog/Desktop/PYTHON/textfile/about_python.txt", "w")
file.write(str)
file.close()
36
url = "/Users/song-yeongseog/Desktop/PYTHON/textfile/"
<w 파일모드>
file = open(url + "hello.txt", "w")
file.write("Hello world!!")
file.close()
<a 파일모드>
file = open(url + "hello.txt", "a")
file.write("\nNice to meet you!!")
file.close()
<x 파일모드>
file = open(url + "hello_01.txt", "x")
file.write("Hello world!!")
file.close()
<w 파일모드>
file = open(url + "hello.txt", "r")
str = file.read()
print(str)
file.close()
<실습>
url = "/Users/song-yeongseog/Desktop/PYTHON/textfile/"
def primeNumber(n):
file = open(url + "prime_numbers.txt", "a")
file.write(str(n))
file.write("\n")
file.close()
inputNumber = int(input("0보다 큰 정수 입력: "))
for number in range(2, (inputNumber+1)):
flag = True
for n in range(2, number):
if number % n == 0:
flag = False
break
if flag:
primeNumber(number)
37
<with ~ as>
url = "/Users/song-yeongseog/Desktop/PYTHON/textfile/"
file = open(url + "5_037.txt", "a")
file.write("python study")
file.close()
file = open(url + "5_037.txt", "r")
print(file.read())
file.close()
with open(url + "5_037.txt", "a") as f:
f.write("python study")
with open(url + "5_037.txt", "r") as f:
print(f.read())
<실습>
import random
url = "/Users/song-yeongseog/Desktop/PYTHON/textfile/"
def writeNumbers(nums):
for idx, num in enumerate(nums):
with open(url + "lotto.txt", "a") as f:
if idx < (len(nums) - 2):
f.write(str(num) + ", ")
elif idx == (len(nums) - 2):
f.write(str(num))
elif idx == (len(nums) - 1):
f.write("\n")
f.write("bonus: " + str(num))
f.write("\n")
rNums = random.sample(range(1, 46), 7)
print(f"rNums: {rNums}")
writeNumbers(rNums)
38
<writelines()>
languages = ["c/c++", "java", "c#", "python", "javascript"]
url = "/Users/song-yeongseog/Desktop/PYTHON/textfile/"
for item in languages:
with open(url + "languages.txt", "a") as f:
f.write(item)
f.write("\n")
languages = ["c/c++", "java", "c#", "python", "javascript"]
url = "/Users/song-yeongseog/Desktop/PYTHON/textfile/"
with open(url + "languages.txt", "a") as f:
f.writelines(item + "\n" for item in languages)
with open(url + "languages.txt", "r") as f:
print(f.read())
<실습>
url = "/Users/song-yeongseog/Desktop/PYTHON/textfile/"
scoreDic = {"kor": 85, "eng": 90, "mat": 92, "sci": 79, "his": 82}
for key in scoreDic.keys():
with open(url + "scoreDic.txt", "a") as f:
f.write(key + "\t: " + str(scoreDic[key]) + "\n")
<실습_scoreDic 그대로 쓰고자 할 때>
scoreDic = {"kor": 85, "eng": 90, "mat": 92, "sci": 79, "his": 82}
with open(url + "scores.txt", "a") as f:
print(scoreDic, file=f)
39
<readlines()>
url = "/Users/song-yeongseog/Desktop/PYTHON/textfile/"
with open(url + "lans.txt", "r") as f:
lanList = f.readlines()
print(f"lanList: {lanList}")
print(f"lanList type: {type(lanList)}")
<readline()>
url = "/Users/song-yeongseog/Desktop/PYTHON/textfile/"
with open(url + "lans.txt", "r") as f:
line = f.readline()
while line != "":
print(f"line: {line}", end="")
line = f.readline()
<실습>
scoreDic = {}
url = "/Users/song-yeongseog/Desktop/PYTHON/textfile/"
with open(url + "score.txt", "r") as f:
line = f.readline()
while line != "":
tempList = line.split(":")
scoreDic[tempList[0]] = int(tempList[1].strip("\n"))
line = f.readline()
print(f"scoreDic: {scoreDic}")
자료출처: 제로베이스 데이터 스쿨