#coding = utf-8
from pprint import pprint
import fileinput
#read(n)
f = open(r'E:\test_dir\somefile.txt', 'r')
a=f.read(7) #读7个字符
b=f.read(4)
print a
print b
f.close()
print
#read()
f = open(r'E:\test_dir\somefile.txt', 'r')
print f.read()
f.close()
print
#readline()
f = open(r'E:\test_dir\somefile.txt')
for i in range(3):
print str(i) + ':'+f.readline()
f.close()
print
##readlines()
pprint(open(r'E:\test_dir\somefile.txt').readlines())
print
#写文件
f = open(r'E:\test_dir\somefile1.txt', 'w')
f.write('this\nis no\nhaiku')
f.close()
f = open(r'E:\test_dir\somefile1.txt', 'r')
print f.readlines()
print
#修改文本文件
#writelines(list)
f = open(r'E:\test_dir\somefile1.txt' )
lines = f.readlines()
f.close()
lines[1]="isn't a\n"
f = open(r'E:\test_dir\somefile1.txt', 'w')
f.writelines(lines)
f.close()
print
def process(string):
print 'Processing:',string
#按字节处理
f = open(r'E:\test_dir\somefile1.txt' )
char =f.read(1)
while char:
process(char)
char =f.read(1)
f.close()
print
#按行操作
f = open(r'E:\test_dir\somefile1.txt' )
while True:
line=f.readline()
if not line:break
process
f.close()
print
#用read迭代每个字符
f = open(r'E:\test_dir\somefile1.txt' )
for char in f.read():
process(char)
f.close()
print
#用readlines迭代行
f = open(r'E:\test_dir\somefile1.txt' )
for line in f.readlines():
process(line)
f.close()
#使用fileinput
for line in fileinput.input(r'E:\test_dir\somefile1.txt'):
process(line)
f.close()
print
#文件迭代器
f = open(r'E:\test_dir\somefile2.txt','w')
f.write('First line\n')
f.write('Second line\n')
f.write('Third line\n')
f.close()
lines = list(open(r'E:\test_dir\somefile2.txt'))
print lines
a,b,c=open(r'E:\test_dir\somefile2.txt')
print a
print b
print c

Python文件和流的更多相关文章

  1. Python进阶篇四:Python文件和流

    摘要: Python对于文件和流的操作与其他编程语言基本差不多,甚至语句上比其他语言更为简洁.文件和流函数针对的对象除了这两者之外还有,类文件(file-like),即python中只支持读却不支持写 ...

  2. (14)python 文件和流

    打开文件 f=open('C:\Temp.txt') 读取数据 f.read(); 关闭文件 f.close();#关闭后将无法再读取 打开文件的方式 不写模式,默认是只读模式 1.r 打开只读文件, ...

  3. Python基础之字符编码,文件操作流与函数

    一.字符编码 1.字符编码的发展史 阶段一:现代计算机起源于美国,最早诞生也是基于英文考虑的ASCII ASCII:一个Bytes代表一个字符(英文字符/键盘上的所有其他字符),1Bytes=8bit ...

  4. 4、Python文件对象及os、os.path和pickle模块(0530)

    文件系统和文件 1.文件系统是OS用于明确磁盘或分区上的文件的方法和数据结构---即在磁盘上组织文件的方法: 文件系统模块:os 2.计算机文件(称文件.电脑档案.档案),是存储在某种长期储存设备或临 ...

  5. python 文件读写操作(24)

    以前的代码都是直接将数据输出到控制台,实际上我们也可以通过读/写文件的方式读取/输出到磁盘文件中,文件读写简称I/O操作.文件I/O操作一共分为四部分:打开(open)/读取(read)/写入(wri ...

  6. python文件的读写总结

    读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘, ...

  7. Python 简明教程 --- 24,Python 文件读写

    微信公众号:码农充电站pro 个人主页:https://codeshellme.github.io 过去的代码都是未经测试的代码. 目录 无论是哪种编程语言,IO 操作都是非常重要的部分.I 即Inp ...

  8. 第九章 Python文件操作

    前一阵子写类相关的内容,把老猿写得心都累了,本来准备继续介绍一些类相关的知识的,如闭包.装饰器.描述符.枚举类.异常等,现在实在不想继续,以后再开章节吧.本章弄点开胃的小菜提提神,介绍Python中文 ...

  9. Linux下Python 文件内容替换脚本

    Linux下Python 文件替换脚本 import sys,os if len(sys.argv)<=4: old_text,new_text = sys.argv[1],sys.argv[2 ...

随机推荐

  1. hdu 3085

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. javaMail 邮件发送和接收示例,支持正文图片、html、附件(转)

    转自:https://blog.csdn.net/star_fly4/article/details/52037587 一.RFC882文档简单说明 RFC882文档规定了如何编写一封简单的邮件(纯文 ...

  3. 中小型研发团队架构实践八:分布式协调服务ZooKeeper

    一.ZooKeeper 是什么 Apache ZooKeeper 由 Apache Hadoop 的子项目发展而来,于 2010 年 11 月正式成为了 Apache 的顶级项目. ZooKeeper ...

  4. 设计模式学习——代理模式(Proxy Pattern)

    放假啦~学生们要买车票回家了,有汽车票.火车票,等.但是,车站很远,又要考试,怎么办呢?找代理买啊,虽然要多花点钱,但是,说不定在搞活动,有折扣呢~ /// /// @file Selling_Tic ...

  5. CentOS安装Oracle 11g R2

    官方的安装链接: https://docs.oracle.com/cd/E11882_01/install.112/e24326/toc.htm#BHCGJCEA 检查硬件需求 1. 内存需求 物理内 ...

  6. Git学习 之 安装

    1.官网下载 https://git-scm.com/downloads 2.修改安装目标路径,其他默认安装 3.通过系统管理员身份打开cmd,输入git 检查是否安装成功

  7. 一步一步pwn路由器之radare2使用全解

    前言 本文由 本人 首发于 先知安全技术社区: https://xianzhi.aliyun.com/forum/user/5274 radare2 最近越来越流行,已经进入 github 前 25了 ...

  8. leetCode题解之根据字符出现的频率排序

    1.题目描述 Given a string, sort it in decreasing order based on the frequency of characters. Example 1: ...

  9. 创建和修改 ExpressRoute 线路的对等互连

    本文将指导你执行相关步骤,以便使用 Azure 门户和 Resource Manager 部署模型创建和管理 ExpressRoute 线路的路由配置. 配置先决条件 在开始配置之前,请务必查看先决条 ...

  10. FileStream对文本进行读写操作

    class FileHelper { /// <summary> /// 检验文件路径是否合法 /// </summary> /// <param name=" ...