본문 바로가기
IT관련/Python

[확고의 쓱~ 훑어보는 기록] Python - 화일입출력 예제(3) (txt파일에 구구단 입력하기 / read, readline, readlines 함수 차이)

by 확고 2022. 8. 2.
728x90
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#-*- coding:utf-8
'''
Created on 2022. 4. 6.
 
@author: hwakgo
'''
 
for i in range(1,10):#i에 1~9까지의 숫자가 반복해서 하나씩 들어감
    data = "3 * %d = %d" %(i,3*i)
    print(data)
#▶3 * 1 = 3
#▶3 * 2 = 6
#▶3 * 3 = 9
#▶3 * 4 = 12
#▶3 * 5 = 15
#▶3 * 6 = 18
#▶3 * 7 = 21
#▶3 * 8 = 24
#▶3 * 9 = 27
    
print('********************')
 
#(문제) gugudan.txt에 구구단을 입력해라
#(정답)
f=open('gugudan.txt''w')
for i in range(1,10):
    data = "3 * %d = %d" %(i,3*i)
    f.write(data)
    f.write('\n')
f.close()
#새로고침하면 gugudan.txt가 생성되고 그 안에 아래와 같이 구구단이 입력됨
#▶3 * 1 = 3
#▶3 * 2 = 6
#▶3 * 3 = 9
#▶3 * 4 = 12
#▶3 * 5 = 15
#▶3 * 6 = 18
#▶3 * 7 = 21
#▶3 * 8 = 24
#▶3 * 9 = 27
 
print('********************')
 
= open('gugudan.txt''r')
while True:
    line = f.readline()#★ open함수 (6) readline() : 한 줄 읽기(자동개행 포함)
    if line == '':#line 변수에 내용이 아무것도 없으면
        break#멈춰라
    print('line:',line,end='')#realine 자체 자동개행이 있기 때문에 end=''를 쓰더라도 결과에 줄바꿈이 있다
f.close()
#▶line: 3 * 1 = 3
#▶line: 3 * 2 = 6
#▶line: 3 * 3 = 9
#▶line: 3 * 4 = 12
#▶line: 3 * 5 = 15
#▶line: 3 * 6 = 18
#▶line: 3 * 7 = 21
#▶line: 3 * 8 = 24
#▶line: 3 * 9 = 27
 
print('********************')
 
= open('gugudan.txt''r')
lines = f.readlines();#★ open함수 (7) readlines() : 한 줄이 list 요소로 들어감(줄바꿈 포함). 배열 같은 list임
print('lines:',lines)
#▶lines: ['3 * 1 = 3\n', '3 * 2 = 6\n', '3 * 3 = 9\n', '3 * 4 = 12\n', '3 * 5 = 15\n', '3 * 6 = 18\n', '3 * 7 = 21\n', '3 * 8 = 24\n', '3 * 9 = 27\n']
 
print('type(lines):',type(lines))
#▶type(lines): <class 'list'>
f.close()
 
print('********************')
 
= open('gugudan.txt''r')
for line in f.readlines():#lines
    print('line:',line,end='')
f.close()
#▶line: 3 * 1 = 3
#▶line: 3 * 2 = 6
#▶line: 3 * 3 = 9
#▶line: 3 * 4 = 12
#▶line: 3 * 5 = 15
#▶line: 3 * 6 = 18
#▶line: 3 * 7 = 21
#▶line: 3 * 8 = 24
#▶line: 3 * 9 = 27
 
print('********************')
 
= open('gugudan.txt','r')
lines = f.read()#read() 파일 포인터 0번째 위치부터~끝까지 읽어옴
print('lines:'+'\n'+lines)
#▶lines:
#▶3 * 1 = 3
#▶3 * 2 = 6
#▶3 * 3 = 9
#▶3 * 4 = 12
#▶3 * 5 = 15
#▶3 * 6 = 18
#▶3 * 7 = 21
#▶3 * 8 = 24
#▶3 * 9 = 27
 
print('type(lines:',type(lines))
#▶type(lines: <class 'str'>
f.close()
 
#readline(), readlines(), read() 차이 숙지
cs

 

▼ (이전 게시물) [확고의 쓱~ 훑어보는 기록] Python - 화일입출력 예제(2) (open 함수 / w모드 / a모드 / r모드 / write / tell / read / seek / close)

 

[확고의 쓱~ 훑어보는 기록] Python - 화일입출력 예제(2) (open 함수 / w모드 / a모드 / r모드 / write / tel

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 #-*- coding:utf-8 ''' Crea..

j-growthdiary.tistory.com

 

▼ (첫 번째 게시물) [확고의 쓱~ 훑어보는 기록] Python - 출력 예제 (print / str, int 함수 / 서식 문자 사용)

 

[확고의 쓱~ 훑어보는 기록] Python - 출력 예제 (print / str, int 함수 / 서식 문자 사용)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 #-*- coding:utf-8 ''' Created on ..

j-growthdiary.tistory.com

728x90
반응형

댓글