File 읽기,쓰기, 삭제
import os
os.system("cls") # 윈도우 화면 클리어 명령어
def write_memo(Filename):
print("메모를 입력하세요. (종료하려면" + "q" +"를 입력하세요)")
lines = [] # 입력받은 메모를 저장할 리스트
while True:
line = input() #사용자로부터 한 줄씩 입력 받기
if line.lower() == 'q':
break
lines.append(line) # 입력받은 줄을 리스트에 추가
with open(Filename, "a") as file:
file.write("\n".join(lines)) # 입력받은 줄을 리스트에 추가
def read_memo(Filename):
with open(Filename, "r") as file: # hello.txt 파일을 읽기 모드(r)로 열기
line = None # 변수 line을 None으로 초기화
while line != '':
line = file.readline()
print(line.strip('\n')) # 파일에서 읽어온 문자열에서 \n 삭제하여 출력
def delete_memo(Filename):
if os.path.exists(Filename): #파일이 존재한다면
os.remove(Filename)
print("파일이 정상적으로 삭제되었습니다.")
else:
print("삭제할 파일 존재하지 않습니다.")
def main():
Filename = "D:\\web\\pywork\\20240610\\memo.txt"
while True:
print("\n 메모장 프로그램")
print ("1. 메모 작성 및 저장")
print ("2. 메모 읽기")
print ("3. 메모 식제")
print ("4. 종료")
choice = input("원하는 작업을 선택 하세요.: ")
if choice == "4":
print("프로그램을 종료합니다.")
break
elif choice == "1":
write_memo(Filename)
os.system("cls") # 윈도우 화면 클리어 명령어
elif choice == "2":
read_memo(Filename)
elif choice == "3":
delete_memo(Filename)
else:
print("올바른 선택이 아닙니다. 다시 선택하세요.")
main()
클래스 사용하기
class PiggyBank:
def __init__(self, money):
self.__money = money #변수 앞에 __를 붙여서 비공개 속성으로 만듦
def pay_info(self, money):
print(f"당신의 저금통에는 {self.__money}원이 있습니다.")
def pay_add(self, money):
if money >= 0:
self.__money += money
print(f"당신의 꿀꿀이통에 {money}원이 입금 되었습니다 현재 잔액은 {self.__money}원이 있습니다.")
else :
print(f"입금할 금액은 0보다 커야 합니다.")
def pay_sub(self, money):
if money > self.__money:
print(f"당신의 꿀꿀이통에 있는 잔액 {self.__money}원 보다 출금 금액({money})이 큽니다.")
else:
self.__money -= money
print(f"당신의 꿀꿀이통에서 {money}가 출금 되었습니다 현재 잔액은 {self.__money}원이 있습니다.")
def main():
global money
money = 0
maria = PiggyBank(money)
while True:
print("\n 꿀꿀이 저금통")
print ("1. 꿀꿀이통 입금")
print ("2. 꿀꿀이통 출금")
print ("3. 꿀꿀이통 조회")
print ("4. 종료")
choice = input("원하는 작업을 선택 하세요.: ")
if choice == "1":
money = int(input("입금할 금액을 입력하세요: "))
maria.pay_add(money)
elif choice == "2":
money = int(input("출금할 금액을 입력하세요: "))
maria.pay_sub(money)
elif choice == "3":
maria.pay_info(money)
elif choice == "4":
print("꿀꿀이 저금통 프로그램을 종료합니다.")
break
else:
print("잘 못 선탟하셨습니다.")
main()
class_attribute.py
0.00MB
class_attribute1.py
0.00MB
class_class_attribute.py
0.00MB
class_private_attribute.py
0.00MB
file_writelines.py
0.00MB
filewritelinestest2.py
0.00MB
fileWritelineTest1.py
0.00MB
function_in_function1.py
0.00MB