Contents
반응형
import sys
print(1)
print('hello',' ','hello')
# sep 파라미터
x =0.2
s='hello'
print(str(x)+' ',s)
print(x,s,sep=' ')
# 기본적인 print()
print(sep=' ',end='\n')
# file 파라미터를 지정
print('hello world',file=sys.stdout)
print('error: hello wold',file=sys.stderr)
# file 출력
f=open('hello.txt','w')
print( type(f) )
print('hello world',file=f)
f.close()
# 참고
sys.stdout.write('Heloo wORld?~')
#textmode 가 default : t
f =open('test.txt','w',encoding='utf-8')
writesize = f.write('안녕하세요\n 파이썬입니다')
f.close()
print(writesize)
# binary mode : b
f = open('test2.txt','wb')
writesize=f.write(bytes('안녕하세요\n 파이썬입니다',encoding='utf-8'))
f.close()
print(writesize)
# read test
f=open('test.txt','rt',encoding='utf-8')
text=f.read()
f.close()
print(text)
# binary read ": copy file
fsrc = open('1.jpg','rb')
data= fsrc.read()
fsrc.close()
print(type(fsrc))
fdest=open('2.jpg','wb')
fdest.write(data)
fdest.close()
f=open('test.txt','rt',encoding='utf-8')
text=f.read()
print(text)
f=open('test.txt','rt',encoding='utf-8')
text=f.read()
print('--------',text,'---------')
#
pos = f.tell()
print(pos)
# (offset, from_what)
# ,0 (맨앞에서)
f.seek(16,0)
text=f.read()
print(text)
# f.seek(10,2)
# text=f.read()
# print(text)
# line 단위로 읽기
linenum=0
f2= open('fileio2.py','rt',encoding='utf-8')
while True :
line = f2.readline()
if line == '':
f2.close()
break
linenum+=1
print('{0}:{1}'.format(linenum,line),end='')
print('============================================')
f3 = open('fileio2.py','rt',encoding='utf-8')
lines=f3.readlines()
f3.close()
print(type(lines))
for linenum,line in enumerate(lines):
print(linenum,':',line)
f=open('123.txt','wb')
f.write(b'0123456789')
f.close()
f=open('123.txt','rb')
print(f.tell())
f.seek(5,1)
data=f.read(2)
print(data)
f.seek(-5,1)
data=f.read(3)
print(data)
f.seek(0,2) # (맨끝 뒤로 이동)
data=f.read(2)
print(data)
# with ~ as
with open('fileio2.py','rt',encoding='utf-8') as f2 :
for linenum,line in enumerate(f2.readlines()) :
print('{0}:{1}'.format(linenum,line),end='')
반응형
'IT 공부 > Python' 카테고리의 다른 글
Python 크롤링 연습 (0) | 2019.06.18 |
---|---|
Python 클래스(1) 클래스, 인스턴스 메서드/ 멤버, 인스턴스 변 (0) | 2019.06.17 |
Python 모듈, import (0) | 2019.06.17 |
Python 기초 str, tuple 등.. (0) | 2019.06.13 |
Python 기초 (0) | 2019.06.12 |