Skip to content

Commit 921ccc0

Browse files
author
yijun_latitude
committed
2017.12.27 edit lesson 5
1 parent d7e8f0d commit 921ccc0

1 file changed

Lines changed: 224 additions & 2 deletions

File tree

python_basic/python_basic_lesson_05.ipynb

Lines changed: 224 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -696,11 +696,233 @@
696696
"\n",
697697
"print(matches)"
698698
]
699+
},
700+
{
701+
"cell_type": "markdown",
702+
"metadata": {},
703+
"source": [
704+
"---\n",
705+
"\n",
706+
"## 文件和目录操作之二\n",
707+
"\n",
708+
"读写文件是最常见的IO操作。Python内置了读写文件的函数,用法和C是兼容的。\n",
709+
"\n",
710+
"读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求操作系统打开一个文件对象,然后,通过操作系统提供的接口从这个文件对象中读取数据,或者把数据写入这个文件对象。\n",
711+
"\n",
712+
"##### 读文件\n",
713+
"\n",
714+
"函数 `open()` 返回 文件对象,通常的用法需要两个参数:`open(filename, mode)`。分别是文件名和打开模式\n",
715+
"\n",
716+
"在做下面的例子前,我们要创建一个 `test.txt` 文件,并且保证其中的内容是如下样式,包含三行内容:\n",
717+
"\n",
718+
"> hello\n",
719+
"\n",
720+
"> hi\n",
721+
"\n",
722+
"> byebye\n",
723+
"\n",
724+
"文件保存在可以访问的目录,我这里就保存在和 notebook 同样的目录\n",
725+
"\n",
726+
"> 使用 jupyter 可以直接新建 Text File,来完成建立和编辑文本文件"
727+
]
728+
},
729+
{
730+
"cell_type": "code",
731+
"execution_count": null,
732+
"metadata": {
733+
"collapsed": true
734+
},
735+
"outputs": [],
736+
"source": [
737+
"import os\n",
738+
"\n",
739+
"# 获得当前路径\n",
740+
"cd = os.getcwd()\n",
741+
"\n",
742+
"print(cd)\n",
743+
"\n",
744+
"# 拼接完整文件名\n",
745+
"filename = os.path.join('/Users/Feng', 'test.txt')\n",
746+
"\n",
747+
"print(filename)\n",
748+
"\n",
749+
"try:\n",
750+
" # 打开文件\n",
751+
" f = open(filename, 'r')\n",
752+
" print(f.read())\n",
753+
"finally:\n",
754+
" if f:\n",
755+
" f.close()"
756+
]
757+
},
758+
{
759+
"cell_type": "code",
760+
"execution_count": null,
761+
"metadata": {
762+
"collapsed": true
763+
},
764+
"outputs": [],
765+
"source": [
766+
"# 简化调用方式\n",
767+
"# 省却了 try...finally,会有 with 来自动控制\n",
768+
"\n",
769+
"with open(filename, 'r') as f:\n",
770+
" print(f.read())"
771+
]
772+
},
773+
{
774+
"cell_type": "code",
775+
"execution_count": null,
776+
"metadata": {
777+
"collapsed": true
778+
},
779+
"outputs": [],
780+
"source": [
781+
"with open(filename, 'r') as f:\n",
782+
" lines = f.readlines()\n",
783+
"\n",
784+
"print(type(lines))\n",
785+
"print(lines)"
786+
]
787+
},
788+
{
789+
"cell_type": "code",
790+
"execution_count": null,
791+
"metadata": {
792+
"collapsed": true
793+
},
794+
"outputs": [],
795+
"source": [
796+
"for i in lines:\n",
797+
" print(i)"
798+
]
799+
},
800+
{
801+
"cell_type": "code",
802+
"execution_count": null,
803+
"metadata": {
804+
"collapsed": true
805+
},
806+
"outputs": [],
807+
"source": [
808+
"# 更简单的按行读取文件内容方法\n",
809+
"with open(filename, 'r') as f:\n",
810+
" for eachline in f:\n",
811+
" print(eachline)"
812+
]
813+
},
814+
{
815+
"cell_type": "markdown",
816+
"metadata": {},
817+
"source": [
818+
"---\n",
819+
"\n",
820+
"##### 写文件\n",
821+
"\n",
822+
"写文件和读文件是一样的,唯一区别是调用 `open()` 函数时,传入标识符 `'w'` 或者 `'wb'` 表示写文本文件或写二进制文件。\n",
823+
"\n",
824+
"r 以读方式打开\n",
825+
"w 以写方式打开\n",
826+
"a 以追加模式打开(必要时候创建新文件)"
827+
]
828+
},
829+
{
830+
"cell_type": "code",
831+
"execution_count": null,
832+
"metadata": {
833+
"collapsed": true
834+
},
835+
"outputs": [],
836+
"source": [
837+
"# 写文件\n",
838+
"import os\n",
839+
"\n",
840+
"# 获得当前路径\n",
841+
"cd = os.getcwd()\n",
842+
"\n",
843+
"# 拼接完整文件名\n",
844+
"filename= os.path.join(cd, 'test2.txt')\n",
845+
"\n",
846+
"# 换行符\n",
847+
"br = os.linesep\n",
848+
"\n",
849+
"# 写文件\n",
850+
"with open(filename, 'w') as f:\n",
851+
" f.write('Hello, World!' + br)\n",
852+
" f.write('Hello, Shanghai!' + br)\n",
853+
" f.write('Hello, CHINA!' + br)\n",
854+
" \n",
855+
"with open(filename, 'r') as f:\n",
856+
" print(f.read())"
857+
]
858+
},
859+
{
860+
"cell_type": "code",
861+
"execution_count": null,
862+
"metadata": {
863+
"collapsed": true
864+
},
865+
"outputs": [],
866+
"source": [
867+
"##### 操作系统和文件系统差异处理\n",
868+
"\n",
869+
"`linesep` 文件中分隔行的字符串\n",
870+
"`path.sep` 分割文件路径名的字符串\n",
871+
"`curdir` 当前工作目录的字符串\n",
872+
"`pardir` 当前工作目录的父目录字符串"
873+
]
874+
},
875+
{
876+
"cell_type": "markdown",
877+
"metadata": {},
878+
"source": [
879+
"---\n",
880+
"\n",
881+
"##### 使用 glob 包查找文件\n",
882+
"\n",
883+
"glob 是 python 自己带的一个文件操作相关模块,很简洁,用它可以查找符合自己目的的文件,就类似于Windows下的文件搜索,而且也支持通配符: `*,?,[]` 这三个通配符,\\* 代表0个或多个字符,? 代表一个字符,[] 匹配指定范围内的字符,如[0-9]匹配数字。\n",
884+
"\n",
885+
"glob 的主要方法也叫 glob,该方法返回所有匹配的文件路径列表,该方法需要一个参数用来指定匹配的路径字符串"
886+
]
887+
},
888+
{
889+
"cell_type": "code",
890+
"execution_count": null,
891+
"metadata": {
892+
"collapsed": true
893+
},
894+
"outputs": [],
895+
"source": [
896+
"# 使用 glob 来遍历指定路径下的指定类型文件\n",
897+
"import glob\n",
898+
"\n",
899+
"# notebook 写法\n",
900+
"glob.glob('/Users/yijun/dev_python/*/*.py')\n",
901+
"\n",
902+
"# IDLE 写法\n",
903+
"l = glob.glob('/Users/yijun/dev_python/*/*.py')\n",
904+
"for i in l:\n",
905+
" print(i)"
906+
]
907+
},
908+
{
909+
"cell_type": "code",
910+
"execution_count": null,
911+
"metadata": {
912+
"collapsed": true
913+
},
914+
"outputs": [],
915+
"source": [
916+
"l = glob.glob('/Users/yijun/dev_python/*/e2*.py')\n",
917+
"for i in l:\n",
918+
" print(i)"
919+
]
699920
}
700921
],
701922
"metadata": {
923+
"anaconda-cloud": {},
702924
"kernelspec": {
703-
"display_name": "Python 3",
925+
"display_name": "Python [default]",
704926
"language": "python",
705927
"name": "python3"
706928
},
@@ -714,7 +936,7 @@
714936
"name": "python",
715937
"nbconvert_exporter": "python",
716938
"pygments_lexer": "ipython3",
717-
"version": "3.4.5"
939+
"version": "3.5.2"
718940
}
719941
},
720942
"nbformat": 4,

0 commit comments

Comments
 (0)