1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+
4+ < head >
5+ < meta charset ="utf-8 ">
6+
7+ < title > Python VI: File</ title >
8+
9+ < link rel ="icon " type ="image/x-icon " href ="../../images/favicon.ico ">
10+ < link rel ="shortcut icon " type ="image/x-icon " href ="../../images/favicon.ico ">
11+
12+ < meta name ="apple-mobile-web-app-capable " content ="yes " />
13+ < meta name ="apple-mobile-web-app-status-bar-style " content ="black-translucent " />
14+
15+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui ">
16+
17+ < link rel ="stylesheet " href ="../css/reveal.css ">
18+ < link rel ="stylesheet " href ="../css/theme/black.css " id ="theme ">
19+
20+ <!-- Code syntax highlighting -->
21+ < link rel ="stylesheet " href ="../lib/css/zenburn.css ">
22+
23+ <!-- Printing and PDF exports -->
24+ < script >
25+ var link = document . createElement ( 'link' ) ;
26+ link . rel = 'stylesheet' ;
27+ link . type = 'text/css' ;
28+ link . href = window . location . search . match ( / p r i n t - p d f / gi ) ? '../css/print/pdf.css' : '../css/print/paper.css' ;
29+ document . getElementsByTagName ( 'head' ) [ 0 ] . appendChild ( link ) ;
30+ </ script >
31+
32+ <!--[if lt IE 9]>
33+ <script src="lib/js/html5shiv.js"></script>
34+ <![endif]-->
35+ </ head >
36+
37+ < body >
38+ < div class ="reveal ">
39+ < div class ="slides ">
40+ <!-- Intro -->
41+ < section >
42+ < section >
43+ < h1 > Python</ h1 >
44+ < h2 > File I/O</ h2 >
45+ < h5 > 2019/12/5 林弘祥</ h5 >
46+ </ section >
47+ < section >
48+ < h2 > 課程簡介</ h2 >
49+ < ul >
50+ < li > File Object</ li >
51+ < li > JSON</ li >
52+ < li > CSV</ li >
53+ </ ul >
54+ </ section >
55+ </ section >
56+ <!-- File Object -->
57+ < section >
58+ < h2 > File Object</ h2 >
59+ < pre > < code class ="python "> FileObject = open('FILENAME', MODE, BUFFERING, ENCODING)</ code > </ pre >
60+ </ section >
61+ < section >
62+ < section data-markdown >
63+ < script type ="text/template ">
64+ ### 開啟 / 關閉
65+
66+ `` `python
67+ # 開啟 test.txt 檔案
68+ File = open('test.txt', 'r') # 'r': open for reading (default)
69+
70+ # 關閉檔案
71+ File.close()
72+ ` ``
73+ </ script >
74+ </ section >
75+ < section data-markdown >
76+ < script type ="text/template ">
77+ ### 模式設定
78+
79+ | MODE | Readable | Writable | Overwrites |
80+ | :- : | :- : | :- : | :- : |
81+ | r | O | | |
82+ | w | | o | o |
83+ | x | | o | |
84+ | a | o | o | |
85+ </ script >
86+ </ section >
87+ < section data-markdown >
88+ < script type ="text/template ">
89+ * In 'x' mode , if file exist , raise FileExistsError
90+ * File mode : 'b' , 't' , '+'
91+ - 'b' : Binary Mode
92+ - 't' : Text Mode
93+ - '+' : Open a disk file for updating ( read / write )
94+ </ script >
95+ </ section >
96+ < section data-markdown >
97+ < script type ="text/template ">
98+ ### 自動進行資源管理
99+
100+ `` `python
101+ with open(FILENAME) as FILE:
102+ # DOSOMETHING AT HEAR
103+ ` ``
104+
105+ * 使用完後系統會自己回收相關資源
106+ * 避免資源占用 , 互鎖 . . . 等錯誤
107+ </ script >
108+ </ section >
109+ </ section >
110+ <!-- I/O -->
111+ < section >
112+ < section data-markdown >
113+ < script type ="text/template ">
114+ ### 讀 / 寫
115+
116+ `` `python
117+ # 嘗試讀入COUNT位資料, 若沒設COUNT, 嘗試讀到能讀的最大量
118+ FILE.read(COUNT)
119+
120+ # 嘗試讀入一行資料
121+ FiILE.readline()
122+
123+ # 嘗試讀入COUNT行資料, 若沒設COUNT, 嘗試讀到能讀的最大量
124+ FiILE.readlines(COUNT)
125+
126+ # 嘗試寫入資料
127+ FILE.write(STRING)
128+
129+ # 嘗試寫入多行資料
130+ FiILE.writelines(LIST)
131+ ` ``
132+ </ script >
133+ </ section >
134+ <!-- Etc. -->
135+ < section data-markdown >
136+ < script type ="text/template ">
137+ ### 其他操作
138+
139+ * . tell ( ) : 目前游標位置
140+ * . seek ( ...) : 移動游標到 . . . 位置
141+ * . name : 檔案名稱
142+ * . mode : 目前操作模式
143+ * . closed : 檔案是否關閉
144+ </ script >
145+ </ section >
146+ </ section >
147+ <!-- JSON -->
148+ < section >
149+ < section >
150+ < h2 > 使用 JSON 格式進行檔案存取</ h2 >
151+ < pre > < code class ="python "> import json</ code > </ pre >
152+ </ section >
153+ < section data-markdown >
154+ < script type ="text/template ">
155+ `` `python
156+ import json
157+
158+ with open('test.txt', 'w') as File:
159+ Data = {'NAME': 'OWO', 'LIST': [1, 3, 4], 'FLOAT': 3.14}
160+ json.dump(Data, File)
161+
162+ with open('test.txt', 'r') as File:
163+ Data = json.load(File)
164+ print(type(Data), Data)
165+ print(type(Data['LIST']), Data['LIST'])
166+ ` ``
167+ </ script >
168+ </ section >
169+ </ section >
170+ <!-- CSV -->
171+ < section >
172+ < section >
173+ < h2 > 使用 CSV 格式進行檔案存取</ h2 >
174+ < pre > < code class ="python "> import csv</ code > </ pre >
175+ </ section >
176+ </ section >
177+ </ div >
178+ </ div >
179+ < script src ="../lib/js/head.min.js "> </ script >
180+ < script src ="../js/reveal.js "> </ script >
181+ < script >
182+ // Full list of configuration options available at:
183+ // https://github.com/hakimel/reveal.js#configuration
184+ Reveal . initialize ( {
185+ controls : true ,
186+ progress : true ,
187+ history : true ,
188+ center : true ,
189+ transition : 'slide' , // none/fade/slide/convex/concave/zoom
190+ // Optional reveal.js plugins
191+ dependencies : [
192+ { src : '../lib/js/classList.js' , condition : function ( ) { return ! document . body . classList ; } } ,
193+ { src : '../plugin/markdown/marked.js' , condition : function ( ) { return ! ! document . querySelector ( '[data-markdown]' ) ; } } ,
194+ { src : '../plugin/markdown/markdown.js' , condition : function ( ) { return ! ! document . querySelector ( '[data-markdown]' ) ; } } ,
195+ { src : '../plugin/highlight/highlight.js' , async : true , condition : function ( ) { return ! ! document . querySelector ( 'pre code' ) ; } , callback : function ( ) { hljs . initHighlightingOnLoad ( ) ; } } ,
196+ { src : '../plugin/zoom-js/zoom.js' , async : true } ,
197+ { src : '../plugin/notes/notes.js' , async : true }
198+ ]
199+ } ) ;
200+ </ script >
201+ </ body >
202+ </ html >
0 commit comments