一打开文件

二操作文件

三关闭文件

open(文件,模式,编码),打开文件----->0101010(以二进制的方式打开)------>编码(open默认utf-8编码)------>显示

  • r ,只读模式【默认】
  • w,只写模式【不可读;不存在则创建;存在则清空内容;】
  • x, 只写模式【不可读;不存在则创建,存在则报错】
  • a, 追加模式【可读;   不存在则创建;存在则只追加内容;】

"+" 表示可以同时读写某个文件

  • r+, 读写【可读,可写】指针在起始位置
  • w+,写读【可读,可写】指针在起始位置
  • x+ ,写读【可读,可写】指针在起始位置
  • a+, 写读【可读,可写】读取的时候指针会在最后这样就是追加文件内容,如果此时读取文件内容,那么将显示空

"b"表示以字节的方式操作,注意b是操作字节这里就不需要编码了。打开文件----------->010101010--------->显示。这里没有编码过程。要是想显示出来就的将二进制转成特定的编码才可以。str("二进制对象001010101",encoding="utf-8"),这样就对二进制进行了编码翻译我们就可以正常读取了。

  • rb  或 r+b
  • wb 或 w+b
  • xb 或 w+b
  • ab 或 a+b

注:以b方式打开时,读取到的内容是字节类型,写入时也需要提供字节类型.bytes("你好",encoding="utf-8")将字符串转换成二进制

r/w/x/a  =>字符串格式

rb/wb/xb/ab => 字节类型(二进制)soket

中文占用3个字符,

查看指针

f.open('xxxx.log','a',encoding='utf-8')

f.tall()

调整指针位置

f.seek(num)

文件方法介绍:

 class file(object):
"""
file(name[, mode[, buffering]]) -> file object Open a file. The mode can be 'r', 'w' or 'a' for reading (default),
writing or appending. The file will be created if it doesn't exist
when opened for writing or appending; it will be truncated when
opened for writing. Add a 'b' to the mode for binary files.
Add a '+' to the mode to allow simultaneous reading and writing.
If the buffering argument is given, 0 means unbuffered, 1 means line
buffered, and larger numbers specify the buffer size. The preferred way
to open a file is with the builtin open() function.
Add a 'U' to mode to open the file for input with universal newline
support. Any line ending in the input file will be seen as a '\n'
in Python. Also, a file so opened gains the attribute 'newlines';
the value for this attribute is one of None (no newline read yet),
'\r', '\n', '\r\n' or a tuple containing all the newline types seen. 'U' cannot be combined with 'w' or '+' mode.
"""
def close(self): # real signature unknown; restored from __doc__
关闭文件
pass def fileno(self): # real signature unknown; restored from __doc__
文件描述符,这个在soket io中会有用,检测文本或soket是否有变化就是靠这个
return 0 def flush(self): # real signature unknown; restored from __doc__
将内存数据刷新到硬盘
pass def isatty(self): # real signature unknown; restored from __doc__
判断文件是否是同意tty设备
return False def next(self): # real signature unknown; restored from __doc__
获取下一行数据,不存在,则报错
pass def read(self, size=None): # real signature unknown; restored from __doc__
读取数据,可以选择读取的字符串或字节大小
pass def readinto(self): # real signature unknown; restored from __doc__
读取到缓冲区,不要用,将被遗弃
pass def readline(self, size=None): # real signature unknown; restored from __doc__
仅读取一行数据,节约内存资源
pass def readlines(self, size=None): # real signature unknown; restored from __doc__
读取所有数据,并根据换行保存值列表
return [] def seek(self, offset, whence=None): # real signature unknown; restored from __doc__
指定文件中指针位置
pass def tell(self): # real signature unknown; restored from __doc__
获取指针位置
pass def truncate(self, size=None): # real signature unknown; restored from __doc__
截取数据,截取指针位置之前的数据
pass def write(self, p_str): # real signature unknown; restored from __doc__
写入数据
pass def writelines(self, sequence_of_strings): # real signature unknown; restored from __doc__
将一个字符串列表写入文件
pass def xreadlines(self): # real signature unknown; restored from __doc__
可用于逐行读取文件,非全部
pass

python2.7 file

提示:x.read()一次性读取所有数据到内存,如果对象很大那么这样做很不好

下面的方法也是一行一行读取

a=f.open("xxx.log",'r')

for line in a:

print line

with 方法读写文件

with open('xxx.log','r') as f:    等同于 a=f.open("xxx.log",'r')

f.read()

使用with 不需要写close,它会帮你自己动关闭

在python2.6以后with同时可以打开2个文件

with open('xxx.log','r') as f,with open('xxx.log1','r') as f1:

open -python操作文件的更多相关文章

  1. Python操作文件、文件夹、字符串

    Python 字符串操作 去空格及特殊符号 s.strip().lstrip().rstrip(',') 复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sSt ...

  2. Python操作文件和目录

    Python操作文件和目录 读写文件比较简单,有一点特别注意就好了 windows下Python默认打开的文件以gbk解码,而一般我们的文件是utf-8编码的,所以如果文本含有中文,就会出现异常或者乱 ...

  3. python操作文件练习,配置haproxy

    在使用python操作文件的时候,特别是对于网络设备,通常操作配置文件,会简化配置量,配置文件加载到内存中,运行时使用的是内存中的配置,内存中配置修改后立即生效,如果不将配置内容保存到硬盘中,则下次重 ...

  4. Python操作文件-20181121

    Python操作文件 Python操作文件和其他语言一样,操作的过程无非是先定位找到文件.打开文件,然后对文件进行操作,操作完成后关闭文件即可. 文件操作方式:对文件进行操作,主要就是读.写的方式,p ...

  5. 使用python操作文件实现购物车程序

    使用python操作文件实现购物车程序 题目要求如下: 实现思路 始终维护一张字典,该字典里保存有用户账号密码,购物车记录等信息.在程序开始的时候读进来,程序结束的时候写回文件里去.在登录注册的部分, ...

  6. 用Python操作文件

    用Python操作文件 用word操作一个文件的流程如下: 1.找到文件,双击打开. 2.读或修改. 3.保存&关闭. 用Python操作文件也差不多: f=open(filename) # ...

  7. python操作文件案例二则

    前言 python 对于文件及文件夹的操作. 涉及到 遍历文件夹下所有文件 ,文件的读写和操作 等等. 代码一 作用:查找文件夹下(包括子文件夹)下所有文件的名字,找出 名字中含有中文或者空格的文件 ...

  8. Python操作文件文档

    需要帮老师将44G的图书分类一下,人工当然累死了.所以用Python大法处理一下. 思路是读取文件目录下的书名,然后去百度百科查分类,如果还没有就去豆瓣,当当查.哪一个先找到其余的就不用找了.如果没有 ...

  9. Python 操作文件、文件夹、目录大全

    # -*- coding: utf-8 -*- import os import shutil # 一. 路径操作:判断.获取和删除 #1. 得到当前工作目录,即当前Python脚本工作的目录路径: ...

随机推荐

  1. C11线程管理:线程创建

    1.线程的创建 C11创建线程非常简单,只需要提供线程函数就行,标准库提供线程库,并可以指定线程函数的参数. #include <iostream> #include <thread ...

  2. iOS 监听UILabel点击

    label.userInteractionEnabled = YES; // 一定要设置 [label addGestureRecognizer:[[UITapGestureRecognizer al ...

  3. Elasticsearch技术解析与实战(六)Elasticsearch并发

    乐观锁与悲观锁 图示的冲突过程,其实就是es的并发冲突问题,会导致数据不准确 当并发操作es的线程越多,或者读取一份数据,供用户查询和操作的时间越长,在这段时间里,如果数据被其他用户修改,那么我们拿到 ...

  4. 【BZOJ】2142 礼物

    [算法]中国剩余定理 [题意]给定n件物品分给m个人,每人分到wi件,求方案数%p.p不一定是素数. [题解] 首先考虑n全排列然后按wi划分成m份,然后对于每份内都是全排列,除以wi!消除标号影响, ...

  5. 机器学习-kNN-寻找最好的超参数

    一 .超参数和模型参数 超参数:在算法运行前需要决定的参数 模型参数:算法运行过程中学习的参数 - kNN算法没有模型参数- kNN算法中的k是典型的超参数 寻找好的超参数 领域知识 经验数值 实验搜 ...

  6. python初步学习-python模块之 logging

    logging 许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪.在python中,我们不需要第三方的日志组件,python为我们提供了简单易用.且 ...

  7. pycharm显示行号

    在PyCharm 里,显示行号有两种办法: 1,临时设置.右键单击行号处,选择 Show Line Numbers. 但是这种方法,只对一个文件有效,并且,重启PyCharm 后消失. 2,永久设置. ...

  8. flask插件系列之flask_cors跨域请求

    前后端分离在开发调试阶段本地的flask测试服务器需要允许跨域访问,简单解决办法有二: 使用flask_cors包 安装 pip install flask_cors 初始化的时候加载配置,这样就可以 ...

  9. Linux内核中的常用宏container_of其实很简单【转】

    转自:http://blog.csdn.net/npy_lp/article/details/7010752 开发平台:Ubuntu11.04 编 译器:gcc version 4.5.2 (Ubun ...

  10. sqlite3_get_table()

    { sqlite3 *db; char *errmsg=NULL;    //用来存储错误信息字符串 char ret=0; int my_age=0;    //类型根据要提取的数据类型而定 cha ...