一打开文件

二操作文件

三关闭文件

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. K-means的缺点(优化不仅仅是最小化误差)

    K-means的缺点(优化不仅仅是最小化误差) #转载时,请注明英文原作David Robinson,译者Ding Chao.# 我最近遇到一个交叉验证的问题,我认为这个给我提供了一个很好的机会去用“ ...

  2. 【BZOJ】1778: [Usaco2010 Hol]Dotp 驱逐猪猡

    [题意]给定无向图,炸弹开始在1,在每个点爆炸概率Q=p/q,不爆炸则等概率往邻点走,求在每个点爆炸的概率.n<=300. [算法]概率+高斯消元 [题解]很直接的会考虑假设每个点爆炸的概率,无 ...

  3. 如何阻止自动更新‘updated_at’和'created_at'

    可以在模版中添加一条代码: public $timestamps = false;

  4. HTML5实现仪表盘、温度计等插件实用源码

  5. 23、Xpath

    1.什么是Xpath?1.XPath即为XMLPath的简称,它是一种用来确定XML文档中某部分位置的语言.2.HTML可以看做是XML的一种实现,所以selenium用户可以使用这种强大的语言在we ...

  6. div遮罩实现禁用鼠标(click、hover等)事件

    这两天在帮老师做网页,今天想实现在一块区域内禁止鼠标的各种事件,本来是想在框架模板的js文件里去修改,但是改代码的时候有点凌乱...感觉应该自己把问题想复杂了. 所以想了想要是能实现在一个区域内(如: ...

  7. hibernate CasCade deleted object ould be re-saved by cascade

    这个问题个人认为看你用的那种方式,如果是注解式的 比如: @ManyToMany(cascade={CascadeType.MERGE,CascadeType.REFRESH,CascadeType. ...

  8. xv6/bootasm.S + xv6/bootmain.c

    xv6/bootasm.S #include "asm.h" #include "memlayout.h" #include "mmu.h" ...

  9. Ubuntu10.04 下安装RabbitVCS

    安装RabbitVCS的方法步骤如下: 1.sudo add-apt-repository ppa:rabbitvcs/ppa       #将rabbitvcs的添加到源里面.(次操作会提示是否要添 ...

  10. 2017 SWERC

    2017 SWERC A:Cakey McCakeFace 题目描述:有一个炉每次只能放一个蛋糕,炉的进口和出口各放了一个探测器,当放蛋糕进去时,进口的探测器会记录时刻,当蛋糕做好后,蛋糕从出口出来, ...