master
/ 8.1 文件的打开与关闭.ipynb

8.1 文件的打开与关闭.ipynb @master

d487d71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ab69a
d487d71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ab69a
d487d71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ab69a
d487d71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ab69a
d487d71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ab69a
d487d71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ab69a
d487d71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ab69a
 
 
 
d487d71
 
 
 
c7ab69a
d487d71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ab69a
d487d71
 
 
 
 
 
 
 
c7ab69a
 
d487d71
 
 
c7ab69a
 
 
 
 
 
 
 
 
 
 
 
d487d71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ab69a
d487d71
c7ab69a
d487d71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 文件的打开与关闭"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "不论是文本文件还是二进制文件,文件进行写入或读取操作,一般都可以分为以下三步:<br />\n",
    "(1)打开文件并创建文件对象;open()<br />\n",
    "(2)通过文件对象对文件中的内容进行读取和写入等操作;<br />\n",
    "(3)关闭并保存文件内容。close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fr = open(file, mode='r', encoding=None)  # fr为变量名,是用户为文件对象所命名字\n",
    "fr.close()                                # 关闭所创建的文件对象,fr为文件对象名"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "内容管理器由关键字“with”和“as”联合启动,将文件打开操作置于内容管理器中,不需要用f.close()显式的关闭文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(file, mode='r', encoding=None) as fr:  # fr为变量名,是用户为文件对象所命名字\n",
    "    pass                                         # 在缩进区域写对文件对象进行操作的语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "file 参数是一个带路径的文件名:<br />\n",
    "\n",
    "### 绝对路径:<br />\n",
    "从根目录开始,如'c:\\\\temp\\\\temp.txt'<br />\n",
    "\n",
    "### 相对路径:  \n",
    "相对当前文件位置的路径,如:  \n",
    "./temp.txt .表示当前目录,..表示上级目录)<br />\n",
    "../data/temp.txt ..表示上级目录,../data/表示当前文件上级目录的子目录,即当前文件同级的子目录)<br />\n",
    "\n",
    "当打开的文件与当前程序文件在同一路径下时,不需要写路径,一般建议使用相对路径。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用绝对路径时,若反斜杠与后面字符可构成转义字符时,需使用两个反斜杠表示。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'c:\\temp\\next.txt'\n",
    "print('c:\\temp\\next.txt')\n",
    "# 输出结果为: \n",
    "# c:\temp\n",
    "# ext.txt\n",
    "\n",
    "# “\\t”是转义字符,所以需要写成\n",
    "'c:\\\\temp\\\\next.txt'\n",
    "print('c:\\\\temp\\\\next.txt')  # 输出结果为: c:\\temp\\next.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "或是在路径字符串前面加“r”,明确告诉编译器这个字符串是个原生字符串,不要转义,从而避免解析错误。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('c:\\temp\\next.txt')  \n",
    "# 输出结果为: \n",
    "# c:\temp\n",
    "# ext.txt\n",
    "\n",
    "# 前面加r构建原生字符串,不转义\n",
    "print(r'c:\\temp\\next.txt')  # 输出结果为: c:\\temp\\next.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "mode 参数是可选参数,用于指定文件打开的方式和类型  \n",
    "该参数可以使用的符号包括:  \n",
    "'r'、'w'、'x'、'a'、'b'、't'、'+'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| 符号 | 含义 |\n",
    "| :---- | :---- |\n",
    "| 'r' | 以只读模式打开文件(默认值) |\n",
    "| 'w' | 以写数据模式打开文件,该文件已存在,先清除该文件中所有内容;该文件不存在,先创建该文件再打开。 |\n",
    "| 'x' | 以创建文件写数据模式打开文件,该文件已存在,打开失败。 |\n",
    "| 'a' | 以追加写数据模式打开文件,该文件已存在,新数据追加在现有数据之后;该文件不存在,先创建文件后在打开。 |\n",
    "| 'b' | 以二进制模式打开文件处理数据 |\n",
    "| 't' | 以文本模式打开文件处理数据(默认模式) |\n",
    "| '+' | 打开文件并允许更新,相当于增加读或写模式(与 <font color='red'>'r'、'w'  'a'   组合使用</font>,如  'r+'可读可写、'w+'可写可读、'a+'可追加写,可读) |\n"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "1. mode 参数缺省时使用默认值'r',以只读方式打开。\n",
    "2. encoding 参数缺省时时使用当前操作系统默认编码类型(中文 windows10 一般默认 GBK 编码,Mac 和Linux等一般默认编码为UTF-8 编码)  \n",
    "   用二进制对象返回文件内容时,encoding 参数不可用。"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "读取\"What is New in Python3.11.txt\"文件内容:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[What is New in Python3.11.txt](images/ch8/What is New in Python3.11.txt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Summary – Release highlights\n",
      "New Features\n",
      "New Features Related to Type Hints\n",
      "Other Language Changes\n",
      "Other CPython Implementation Changes\n",
      "New Modules\n",
      "Improved Modules\n",
      "Optimizations\n",
      "Faster CPython\n",
      "CPython bytecode changes\n",
      "Deprecated\n",
      "Pending Removal in Python 3.12\n",
      "Removed\n",
      "Porting to Python 3.11\n",
      "Build Changes\n",
      "C API Changes\n"
     ]
    }
   ],
   "source": [
    "new = open('images/ch8/What is New in Python3.11.txt')  # 以读模式打开文件,创建文件对象,命名为new\n",
    "print(new.read())  # 读文件为字符串输出\n",
    "new.close()        # 关闭文件对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Summary – Release highlights\n",
      "New Features\n",
      "New Features Related to Type Hints\n",
      "Other Language Changes\n",
      "Other CPython Implementation Changes\n",
      "New Modules\n",
      "Improved Modules\n",
      "Optimizations\n",
      "Faster CPython\n",
      "CPython bytecode changes\n",
      "Deprecated\n",
      "Pending Removal in Python 3.12\n",
      "Removed\n",
      "Porting to Python 3.11\n",
      "Build Changes\n",
      "C API Changes\n"
     ]
    }
   ],
   "source": [
    "with open('images/ch8/What is New in Python3.11.txt') as new:  # 以读模式打开文件,创建文件对象,命名为new\n",
    "    print(new.read())  # 读文件为字符串输出\n",
    "    \n",
    "# 退出缩进时自动关闭文件"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "读取并输出文件“9.1 出塞.txt”中的内容"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[9.1 出塞.txt](images/ch8/9.1 出塞.txt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "出塞\n",
      "秦时明月汉时关,万里长征人未还。\n",
      "但使龙城飞将在,不教胡马度阴山。\n",
      "\n"
     ]
    }
   ],
   "source": [
    "fr = open('images/ch8/9.1 出塞.txt', 'r', encoding='utf-8')  # 打开文件创建文件对象,命名为fr\n",
    "print(fr.read())  # 读文件为字符串输出\n",
    "fr.close()        # 关闭文件对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "出塞\n",
      "\n",
      "秦时明月汉时关,万里长征人未还。\n",
      "\n",
      "但使龙城飞将在,不教胡马度阴山。\n",
      "\n"
     ]
    }
   ],
   "source": [
    "fr = open('images/ch8/9.1 出塞.txt', 'r', encoding='utf-8')  # 打开文件创建文件对象,命名为fr\n",
    "for a in fr:          # 对文件对象进行逐行遍历\n",
    "    print(a)            # 行末的换行符会输出一个空行\n",
    "fr.close()                # 关闭文件对象\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "出塞\n",
      "秦时明月汉时关,万里长征人未还。\n",
      "但使龙城飞将在,不教胡马度阴山。\n"
     ]
    }
   ],
   "source": [
    "fr = open('images/ch8/9.1 出塞.txt', 'r', encoding='utf-8')  # 打开文件创建文件对象,命名为fr\n",
    "for line in fr:          # 对文件对象进行逐行遍历\n",
    "    print(line.strip())  # line.strip()函数去掉行末的换行符,消除空行\n",
    "fr.close()                # 关闭文件对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "出塞\n",
      "秦时明月汉时关,万里长征人未还。\n",
      "但使龙城飞将在,不教胡马度阴山。\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 用上下文管理器实现,不需要显示关闭文件,避免异常\n",
    "\n",
    "with open('images/ch8/9.1 出塞.txt','r',encoding = 'utf-8') as poem:  # 打开文件创建文件对象,命名为poem\n",
    "    print(poem.read())  # 读文件为字符串输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用上下文管理器实现,不需要显示关闭文件,避免异常\n",
    "\n",
    "with open('images/ch8/9.1 出塞.txt','r',encoding = 'utf-8') as poem:  # 打开文件创建文件对象,命名为poem\n",
    "    for line in poem:          # 对文件对象进行逐行遍历\n",
    "        print(line.strip())  # line.strip()函数去掉行末的换行符,消除空行"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "建议将读文件的代码定义为函数,以方便修改和维护代码:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def open_file(file):  \n",
    "    \"\"\"接收表示文件名的字符串变量,打开文件,遍历读取文件并输出其内容\"\"\"\n",
    "    with open(file,'r',encoding = 'utf-8') as poem:  # 打开文件创建文件对象,命名为poem\n",
    "        for line in poem:          # 对文件对象进行逐行遍历\n",
    "            print(line.strip())  # line.strip()函数去掉行末的换行符,消除空行\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    filename = 'images/ch8/9.1 出塞.txt'  # 文件名定义变量,方便修改\n",
    "    open_file(filename)        # 调用函数读取并输出文件内容"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[What is New in Python3.11.txt](images/ch8/What is New in Python3.11.txt)  \n",
    "[9.1 出塞.txt](images/ch8/9.1 出塞.txt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}