제로베이스

· AboutPython
[readlines()] 파일의 모든 데이터를 읽어서 리스트 형태로 반환한다. from url import * with open(url()+'hello.txt','r') as f : print(f.readlines()) print(type(f.readlines())) ''' ['hello python\n', 'hello c/c++\n', 'hello java\n', 'hello javascript'] ''' 다음과 같이 개행을 포함해서 각 줄을 요소로 하는 리스트를 반환한다. [readline()] .한 행을 읽어서 문자열로 반환한다. from url import * with open(url()+'hello.txt','r') as f : # print(f.readlines()) # print(type(f..
· AboutPython
[writelines()] 반복 가능한 자료형의 데이터를 파일에 쓸 수 있다. 리스트 또는 튜플 데이터를 파일에 쓰기 위한 함수이다. f.write()를 쓰면 from url import * import random with open(url()+'writlines실습.txt','a') as f: randN = random.sample(range(1,46),7) f.write('\n') for i in randN: f.write(f'{i} ') with open(url()+'writlines실습.txt','r') as f: print(f.read()) ''' 38 40 35 25 22 43 7 22 44 41 20 33 2 15 ''' 다음과 같이 리스트의 요소를 f.write()로 쓰려면 반복문을 돌려 ..
· AboutPython
[with ~ as문] 파일 닫기를 생략할 수 있다. 기존에 우리는 이렇게 했었다. from url import * file = open(url()+'practice03.txt','w') file.write('let\' practice with ~ as ~') file.close() with ~ as ~ 를 쓰면 코드가 다음과 같아진다. with open(url()+'practice03.txt','a') as f: f.write('\nthis is the with ~ as 구문') with open(url()+'practice03.txt','r') as f : print(f.read()) ''' let' practice with ~ as ~ this is the with ~ as 구문 ''' close(..
· AboutPython
[파일모드] 'w' : 쓰기 전용 (파일이 있으면 덮어씌움) 'a' : 쓰기 전용 (파일이 있으면 덧붙임) 'x' : 쓰기 전용 (파일이 있으면 에러 발생) 'r' : 읽기 전용 (파일이 없으면 에러 발생) 원래 각 모드에 대해 직접 해보고 일일히 캡쳐 해서 글 다썼지만 임시저장 안해서 다 날라갔기 때문에 바로 실습만 올린다. ㅠㅠ [실습] 사용자가 숫자를 입력하면, 소수를 텍스트 파일에 써보자. 실습 전에 매번 url 적는게 귀찮아서 site-package에 url 관련함수를 넣었다. import sys for path in sys.path: print(path) ''' c:\Users\Jupiter\Desktop\mygit\LearnInZeroBase\파이썬 중급 C:\Users\Jupiter\App..
scone
'제로베이스' 태그의 글 목록 (23 Page)