标签:file gbk 打开 windows close strong 中文 接口 nbsp
文件操作的基本步骤:
1.打开文件:f=open(‘filename‘),with open(‘filename‘) as f
2.操作文件:增,删,改,查
3.关闭文件:f.close
打开文件
python中打开文件的函数为open(‘filename‘,mode=‘r‘,encode=‘utf-8‘),open函数默认返回文件的句柄,我们可以根据句柄来对文件进行增,删,改,查的操作。将句柄赋给我们定义的变量,假设我们定义变量为f,则f=open(‘filename‘,mode=‘r‘,encode=‘utf-8‘) 或者with open(‘filename‘,mode=‘r‘,encode=‘utf-8‘) as f。
注意点:
1.python解释器打开文件时,是对硬盘进行操作,需要内核态才可以操作硬盘,故此时python解释器是调用操作系统的文件读取接口。windows中文版本默认使用GBK编码表,linux默认使用utf-8,所有如果操作的文件在windows下,非GBK编码的,需要在open函数中声明编码类型,使操作系统运用相应的编码规则进行解码读取,防止串码,乱码现象。
2.
标签:file gbk 打开 windows close strong 中文 接口 nbsp
原文地址:http://www.cnblogs.com/white-small/p/6838758.html