- 파일에서 단어 읽기 infile = open("C:\\Users\\Desktop\\happy.txt", "r") for line in infile: line = line.rstrip() word_list = line.split() for word in word_list: print(word) infile.close() - 파일 복사 infilename = input("기존 파일 이름: ") outfilename = input("복사된 파일 이름: ") infile = open(infilename, "r") outfile = open(outfilename, "w") s = infile.read() outfile.write(s) infile.close() outfile.close() shutil 모듈 사..
pickle 모듈 - 프로그램 상에서 우리가 사용하고 있는 객체를 파일 형태로 저장하기 위해 필요한 모듈 - pickle을 사용하기 위해서는 항상 binary 타입 정의해야 함 dump() - 객체를 pickle 모듈로 압축 1. pickle 모듈 포함 2. 객체 생성 3. open() 함수 사용하여 "wb"(이진파일 쓰기) 모드로 파일 열기 4. dump() 함수 호출로 객체 전달 5. 파일 닫기 import pickle # 딕셔너리 gameOption = { "sound":8, "videoQuality":"HIGH", "money":10000, "weaponList":["gun", "missile", "knife"] } # 이진 파일 오픈 file = open("C:\\Users\\Desktop\\s..
실습용 텍스트 파일 - 메모장에 아래 내용 작성 후 바탕화면에 profile.txt로 저장하였다 홍길동 Korean 슈퍼맨 American 셜록 홈즈 British 파일 열기(open) - 파일을 사용하려면 우선 파일을 열어야 함 - 파일 이름을 받아서 파일 객체를 생성한 후 반환 - 파일을 여는 데 실패하면 None 객체 반환 파일 객체 = open(파일이름, 파일모드) # 바탕화면에 있는 profile.txt라는 이름의 파일을 읽기 모드로 열기 infile = open("infile = open("C:\\Users\\Desktop\\profile.txt", "r") ※ 파일 경로의 백슬래시 사용할 때는 반드시 \\ 두번 넣어줘야 함 파일 모드(r, w, a, r+) "r" 읽기 모드(read mode..