Python/Python기초

"""파일을 다루는 데 다음 순서를 준수해야 한다.1) open file2) read/write file3) close file""" # 파일 열기 'w' 는 write 모드 f = open('test.txt', 'w') # 파일에 텍스트를 씀 for i in range(1, 11): f.write(f'{i}번째 줄 ... \n') # 파일 닫기 f.close() # 인코딩 오류 발생시 File - Setting - File Encodings -에서 다음 항목을 UTF-8로 변경한다. # with 구문: 리소스를 사용한 후 close() 메소드를 자동으로 호출 # with ... as 변수: 실행문 with open('test2.txt', mode = 'w', encoding= 'utf-8') as f:..
# 파일 디렉토리 다루기, [os 모듈] 의 변수와 함수들 import os print(os.getcwd()) # CWD : Current Wroking Directory (현재 작업 디렉토리/폴더) 출력 결과C:\dev\lab-python\lec07_file # 절대 경로 (absolute path)# 시스템의 루트(root)부터 전체 디렉토리 경로를 표시하는 방법# ex) C:\dev\lab-python\lec07_file (Windows - 백슬레시\ 사용) : 현재 디렉토리#.Users/user/Document (MacOS 또는 Linux 운영체제 : 슬레시/ 사용) # 상대 경로 (relative path):# 현재 작업 디렉토리(cwd)를 기준으로 경로를 표시하는 방법# .: 현재 디렉토리, ..
상속(inheritance):부모 클래스로부터 데이터(field)와 기능(method)를 물려받아서자식 클래스에서 사용할 수 있도록 하는 개념 - parent (부모), super(상위), base(기본) class- child (자식), sub(하위), derived(유도) class class Shape: def __init__(self, x=0, y=0): print('Shape.__init__ 호출') self.x = x self.y = y def __repr__(self): return f'Shape(x = {self.x}, y = {self.y})' def move(self, dx, dy): self.x += dx self.y += dy if __name__ == '__main__': shap..
class Rectangle:"""직사각형 클래스""" def __init__(self, width=0, height=0): self.width = width # self.width, self.height = parameter self.height = height # width, height = argument def info(self): print(f'Rectangle(w={self.width}, h={self.height})') if __name__ == '__main__': rect1 = Rectangle(3, 2)# argument를 아무 것도 전달하지 않으면# 모든 parameter는 기본값 (default argument)를 사용하게 됨 print(type(rect1)) print(id(rec..
Codezoy
'Python/Python기초' 카테고리의 글 목록 (14 Page)