open -python操作文件
一打开文件
二操作文件
三关闭文件
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操作文件的更多相关文章
- Python操作文件、文件夹、字符串
Python 字符串操作 去空格及特殊符号 s.strip().lstrip().rstrip(',') 复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sSt ...
- Python操作文件和目录
Python操作文件和目录 读写文件比较简单,有一点特别注意就好了 windows下Python默认打开的文件以gbk解码,而一般我们的文件是utf-8编码的,所以如果文本含有中文,就会出现异常或者乱 ...
- python操作文件练习,配置haproxy
在使用python操作文件的时候,特别是对于网络设备,通常操作配置文件,会简化配置量,配置文件加载到内存中,运行时使用的是内存中的配置,内存中配置修改后立即生效,如果不将配置内容保存到硬盘中,则下次重 ...
- Python操作文件-20181121
Python操作文件 Python操作文件和其他语言一样,操作的过程无非是先定位找到文件.打开文件,然后对文件进行操作,操作完成后关闭文件即可. 文件操作方式:对文件进行操作,主要就是读.写的方式,p ...
- 使用python操作文件实现购物车程序
使用python操作文件实现购物车程序 题目要求如下: 实现思路 始终维护一张字典,该字典里保存有用户账号密码,购物车记录等信息.在程序开始的时候读进来,程序结束的时候写回文件里去.在登录注册的部分, ...
- 用Python操作文件
用Python操作文件 用word操作一个文件的流程如下: 1.找到文件,双击打开. 2.读或修改. 3.保存&关闭. 用Python操作文件也差不多: f=open(filename) # ...
- python操作文件案例二则
前言 python 对于文件及文件夹的操作. 涉及到 遍历文件夹下所有文件 ,文件的读写和操作 等等. 代码一 作用:查找文件夹下(包括子文件夹)下所有文件的名字,找出 名字中含有中文或者空格的文件 ...
- Python操作文件文档
需要帮老师将44G的图书分类一下,人工当然累死了.所以用Python大法处理一下. 思路是读取文件目录下的书名,然后去百度百科查分类,如果还没有就去豆瓣,当当查.哪一个先找到其余的就不用找了.如果没有 ...
- Python 操作文件、文件夹、目录大全
# -*- coding: utf-8 -*- import os import shutil # 一. 路径操作:判断.获取和删除 #1. 得到当前工作目录,即当前Python脚本工作的目录路径: ...
随机推荐
- Elasticsearch Java API 查询
一.查询的时候,需要建立一个SearchRequestBuilder,这里面将给出对于哪一个index或者type进行查询,并且所有的设置都可以在这里面进行实现,例如模糊查询,范围查询,前缀查询等. ...
- bootstrap下laydate样式错乱问题
查看发现bs使用了 * {box-sizing:border-box;}重置了盒子模型 那么我们再把它重置回来,在样式中加入以下代码 .laydate_box, .laydate_box * { bo ...
- 朋友封装的一个ASP.NET上传文件的方法
朋友做了asp.net开发多年,做了这个,自我感觉封装得还不错!!! 代码如下: #region 上传文件的方法 /// <summary> /// 上传文件方法 /// </sum ...
- Lua的各种资源1
Libraries And Bindings LuaDirectory > LuaAddons > LibrariesAndBindings This is a list of l ...
- 【BZOJ3745】Norma [分治]
Norma Time Limit: 20 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description Input 第1行,一个整数N: ...
- 基于canvas的图片编辑合成器
在我们日常的前端开发中,经常会要给服务器上传图片,但是局限很大,图片只能是已有的,假设我想把多张图片合成一张上传就需要借助图片编辑器了,但是现在我们有了canvas合成就简单多了 首先我们看图片编辑器 ...
- Date对象相关函数使用
参考:http://www.w3school.com.cn/jsref/jsref_obj_date.asp
- Loadrunner脚本学习总结
1.1 web脚本录制选择Web(HTTP/HTML)协议: 注意录制脚本前选择如下协议: 1.2 脚本如果需要使用如下函数: web_reg_save_param.web_fin ...
- java.lang.IllegalArgumentException: Page directive: invalid value for import
我的项目原来用的tomcat版本是apache-tomcat-7.0.53,后来为了安全原因将版本升至\apache-tomcat-7.0.57,发现有的jsp页面出现下面的异常: java.lang ...
- asp基础
0.1在浏览器中通过查看源代码的方式是无法看到 ASP 源代码的,你只能看到由 ASP 文件输出的结果,而那些只是纯粹的 HTML 而已.这是因为,在结果被送回浏览器前,脚本已经在服务器上执行了. 0 ...