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脚本工作的目录路径: ...
随机推荐
- 重构改善既有代码设计--重构手法11:Move Field (搬移字段)
你的程序中,某个字段被其所驻类之外的另一个类更多的用到.在目标类建立一个新字段,修改源字段的所有用户,令它们改用新字段. 动机:在类之间移动状态和行为,是重构过程中必不可少的措施.随着系 ...
- asp.net 遍历文件夹下全部子文件夹并绑定到gridview上
遍历文件夹下所有子文件夹,并且遍历配置文件某一节点中所有key,value并且绑定到GridView上 Helper app_Helper = new Helper(); DataSet ds = n ...
- 【费用流】【CODEVS】1227 方格取数2
[算法]最小费用最大流(费用流) [题解] 费用流:http://www.cnblogs.com/onioncyc/p/6496532.html 本题构图: 在有限的k次行走中尽可能多的拿到数字,明显 ...
- Metasploit 一些重要模块使用介绍
本文是"T00LS Metasploit(第一季)"的文档版,是个人在观看视频动手操作的一个记录,仅供学习.文中会介绍Metasploit的一些基本使用:端口扫描.smb扫描.服务 ...
- 爬虫--Scrapy框架的基本使用
流程框架 安装Scrapy: (1)在pycharm里直接就可以进行安装Scrapy (2)若在conda里安装scrapy,需要进入cmd里输入指令conda install scrapy ...
- 给vim安装YouCompleteMe
要安装YouCompleteMe ,vim须支持python.看是否支持,可以在vim中:version 查看, 如果python前有+号,就是支持,减号就是不支持. 如果不支持,需要以编译安装方式重 ...
- Mysql储存过程5: while
循环结构 while create procedure name() begin while 条件 do SQL语句 end while; end$ create procedure aa6() be ...
- 【Python学习】使用BeautifulSoup解析HTML
对于一个最简单的爬虫结构的代码是这样的. 也就是抓取出整个页面,然后创建一个BeautifulSoup对象. from urllib.request import urlopen from bs4 i ...
- git@oschina.net源代码管理使用日记【转】
转自:https://www.cnblogs.com/Juvy/p/3556902.html git的优势: 1 可以创建分支: 2 版本控制是基于每一次提交的,而不需要考虑每次提交了多少个文件. 下 ...
- ELK&ElasticSearch5.1基础概念及配置文件详解【转】
1. 配置文件 elasticsearch/elasticsearch.yml 主配置文件 elasticsearch/jvm.options jvm参数配置文件 elasticsearch/log4 ...