Python基础-文件的基本操作
测试文件fansik内容如下:
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6 文件的操作方法:
def open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True): 文件打开方式如下:
========= ===============================================================
Character Meaning
--------- ---------------------------------------------------------------
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' create a new file and open it for writing
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newline mode (deprecated)
========= =============================================================== 文件读操作:
f = open('fansik', 'r', encoding='utf8')
print(f.read())
f.close()
读取整个文件内容,结果如下:
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
f = open('fansik', 'r', encoding='utf8')
print(f.readline())
f.close()
读取整个文件第一行内容,结果如下:
This is line 1
f = open('fansik', 'r', encoding='utf8')
print(f.readlines())
f.close()
读取整个文件内容,以列表方式显示,结果如下:
['This is line 1\n', 'This is line 2\n', 'This is line 3\n', 'This is line 4\n', 'This is line 5\n', 'This is line 6'] 同时操作多个文件(一下方式操作文件是可以不加f.close()):
number = 0
with open('fansik', 'r', encoding='utf8') as f, open('fansik1', 'w', encoding='utf8') as f1:
for line in f:
number += 1
if number == 5:
line = ''.join([line.strip(), 'fanjinbao\n'])
f1.write(line)
fansik1文件内容如下:
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5fanjinbao
This is line 6
Python基础-文件的基本操作的更多相关文章
- python 基础-文件读写'r' 和 'rb'区别
原文链接: python基础-文件读写'r' 和 'rb'区别 一.Python文件读写的几种模式: r,rb,w,wb 那么在读写文件时,有无b标识的的主要区别在哪里呢? 1.文件使用方式标识 'r ...
- Python学习之路基础篇--08Python基础+ 文件的基本操作和 注册小作业
1 文件的基本操作 #1. 打开文件的模式有(默认为文本模式): r ,只读模式[默认模式,文件必须存在,不存在则抛出异常] w,只写模式[不可读:不存在则创建:存在则清空内容] a, 只追加写模式[ ...
- Python之文件的基本操作
在python中,对文件的基本操作一共有如下四种: 1.打开文件 file_obj = open("文件路径","模式") 常用的打开文件模式有: r:以只读方 ...
- Python基础--文件操作和集合
这篇博客来说一下python对文件的操作. 对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句柄操作文件 3.关闭文件. 现有以下文件file.txt: 我们哭了 ...
- python基础--文件操作实现全文或单行替换
python修改文件时,使用w模式会将原本的文件清空/覆盖.可以先用读(r)的方式打开,写到内存中,然后再用写(w)的方式打开. 替换文本中的taste 为 tasting Yesterday whe ...
- python基础-文件操作
一.文件操作 打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作. 打开文件的模式有: r ,只读模式[默认模式,文件必须存在,不存在则抛出异 ...
- python基础----文件处理
一.文件处理流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 正趣果上果 Interesting fruit fruit 词:郭婞 曲:陈粒 编曲/混音/和声:燕池 萧: ...
- python基础 - 文件读写
完成功能: 从指定位置读文件到控制台 #! /usr/bin/python # coding=utf- 方法一. try: f = open ('/root/python/file/001.txt', ...
- Python基础-文件操作(七)
一.文件基本操作 1.open 打开模式: w模式 写模式write 文件不存在时会创建文件,如果文件已存在则会清空文件 r模式 读模式read 文件不存在就报错,存在则准备读取文件 a模式 追加模式 ...
随机推荐
- Silverlight实例教程 - Validation数据验证开篇
Silverlight 4 Validation验证实例系列 Silverlight实例教程 - Validation数据验证开篇 Silverlight实例教程 - Validation数据验证基础 ...
- linux navicat 过期 解决办法
:~$ cd .navicat:~/.navicat$ rm *.reg:~/.navicat$ rm .update-timestamp:~/.navicat$ rm navicat.crontab ...
- smali语句类的静态成员查看,invoke-virtual、invoke-direct、invoke-super解释
smali举例: .class public Lcom/dataviz/dxtg/common/android/DocsToGoApp; .super Landroid/app/Application ...
- mysql之load语句
LOAD DATA LOCAL INFILE 'data.txt' INTO TABLE tbl_name
- Python内置函数之classmethod()
函数的参数是一个函数: classmethod(func) 作用是,在外部,类对象能够直接调用类方法. 常用来作为装饰器. >>> class C: ... def f(self): ...
- FreeBSD长模式不兼容
二进制转换与此平台上的长模式不兼容.此虚拟环境中的长模式将被禁用.因此需要使用长模式的应用程序将无法正常运行.请参见 http://vmware.com/info?id=152 了解更多详细信息. m ...
- Spring Boot MongoDB JPA 简化开发
使用SpringBoot提供的@Repository接口,可以完成曾经需要大量代码编写和配置文件定制工作.这些以前让新手程序员头疼,让有经验的程序员引以为傲的配置,由于框架的不断完善,变得不那么重要, ...
- Hibernate使用xml文件的每个类层次一张表
通过这种继承策略,我们可以通过单表映射整个层次结构. 这里,在表中创建一个额外的列(也称为discriminator列)来标识该类. 让我们先了解问题.下面给出的整个层次类映射到数据库的一个表中图解说 ...
- Spring入门之通过注解 处理 数据库事务
用Spring 中的事务写的银行转帐的例子:(环境同上一个贴子) 一.表结构: (create table (id int,username varchar(10),salary int);) 二.文 ...
- eclipse 查看源代码
文地址:http://blog.csdn.net/sushengmiyan/article/details/18798473 本文作者:sushengmiyan 我们在使用Eclipse的时候,经常是 ...