python-文件基本操作(一)
一、打开文件的方法:
fp=file("路径","模式")
fp=open("路径","模式")
注意:file()和open()基本相同,且最后要用close()关闭文件。 在python3中,已经没了file()这种方法
二、操作文件的模式:
打开文件有以下几种模式:
r :以只读方式打开文件
w:打开一个文件,只用于写。如果该文件已存在,则会覆盖,如果文件不存在,则会创建一个新文件
a:打开一个文件,用于追加。如果文件已存在,文件指针会放在文件末尾,如果文件不存在,则会创建一个新文件
三、写文件操作
#代码
fp=open('info','w')
fp.write('this is the first line')
fp.write('this is the second line')
fp.write('this is the third line')
f.close() #文件内容结果
this is the first linethis is the second linethis is the third line
注:在写的过程中,内容不会自动换行,如想换行,需要在每个write()中加入"\n",如
#代码
fp=open('info','w')
fp.write('this is the first line\n')
fp.write('this is the second line\n')
fp.write('this is the third line\n')
f.close() #文件内容结果
this is the first line
this is the second line
this is the third line
四、读文件操作
①一次性读取所有read()
>>> fp=open('info.log','r')
>>> print fp.read()
>>> fp.close()
this is the first line
this is the second line
this is the third line
②一次性读取所有,且把结果放入元组中readlines()
>>> fp=open('info.log','r')
>>> print fp.readlines()
>>> fp.close()
['this is the first line \n', 'this is the second line \n', 'this is the third line \n']
③读取一行readline()
>>> fp=open('info.log','r')
>>> print fp.readline()
>>> fp.close()
this is the first line
循环读取内容
>>> fp=file('info.log','r')
>>> for line in fp:
>>> print line
>>> fp.close()
this is the first line
this is the second line
this is the third line
#注:文件中的没一行带有"\n"换行,而使用print的时候,也会自动换行,因此输出结果中会有两次换行,如果想要达到只换行一次的效果,在print line后加入"," 如:
>>> print line,
this is the first line
this is the second line
this is the third line
五、追加文件内容
>>> fp=open('info.log','a')
>>> f.write('this is the append line\n')
>>> f.close()
this is the first line
this is the second line
this is the third line
this is the append line
python-文件基本操作(一)的更多相关文章
- [ Python入门教程 ] Python文件基本操作
本文将python文件操作实例进行整理,以便后续取用. 文件打开和创建 Python中使用open()函数打开或创建文件.open()的声明如下: open(name[, mode[, bufferi ...
- Python文件基本操作及上下文管理
文件基本操作 打开文件:f = open(fole_name,mode = 'r'),传入表示文件路径的字符串,会返回一个文件对象,mode是文件打开模式. 关闭文件:f.close(),调用给定文件 ...
- python文件基本操作(读,写,追加)
一:只读(r) f=('d:\ python的联系文件'') 绝对路径和相对路径(绝对路径:能找到文件开始到结束路径,真实存在的路径,相对路径:在绝对路径一致的情况下新建一个文件) f=open( ...
- [ Python入门教程 ] Python文件基本操作_shutil模块
shutil模块是对os模块中文件操作的补充,提供文件和目录的移动.复制.打包.压缩.解压等功能 shutil常用函数 shutil.copyfile(src, dst) 复制文件, 如果ds ...
- python文件(概念、基本操作、常用操作、文本文件的编码方式)
文件 目标 文件的概念 文件的基本操作 文件/文件夹的常用操作 文本文件的编码方式 01. 文件的概念 1.1 文件的概念和作用 计算机的 文件,就是存储在某种 长期储存设备 上的一段 数据 长期存储 ...
- python学习9—文件基本操作与高级操作
python学习9—文件基本操作与高级操作 1. 文件基本操作 打开文件,获得文件句柄:f = open('filename',encoding='utf-8'),open会查询操作系统的编码方式,并 ...
- 从零开始的Python学习Episode 7——文件基本操作
文件基本操作 一.打开文件 f = open('11','r')#open('file path','mode') 创建一个文件对象 文件有多种打开模式: 1. 'r':新建一个文件对象以只读方式打开 ...
- python文件I/O(转)
Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你 ...
- python 文件操作总结
Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你 ...
- Python基础篇【第2篇】: Python文件操作
Python文件操作 在Python中一个文件,就是一个操作对象,通过不同属性即可对文件进行各种操作.Python中提供了许多的内置函数和方法能够对文件进行基本操作. Python对文件的操作概括来说 ...
随机推荐
- opencv + ffmpeg
opencv2.4.13 与 ffmepg 3.0 一起是可以安装成功的.注意编译ffmpeg时, ./configure --enable-shared 否则会报错. 另外,把以上组合换成ope ...
- pinyin4j的基本使用
PinYin4jUtils工具类代码:http://www.cnblogs.com/jepson6669/p/8856082.html maven中引入依赖 <!-- 引入pinyin4J的依赖 ...
- elasticSearch请求流程图
- SQLAlchemy基本操作和常用技巧
点击打开链接 Python的ORM框架SQLAlchemy基本操作和常用技巧,包含大量实例,非常好的一个学习SQLAlchemy的教程,需要的朋友可以参考下 python编程语言下的一款开源软件.提供 ...
- Cocos2d-js 开发记录:自定义按钮
游戏开发总是有些特殊,一般的预制的UI无法满足要求.其实对于不复杂的功能,与其看文档还不如自己写一个.比如游戏中一个虚拟键盘,其中的按键在按下时会增长,变为原来的两倍高度,在原来高度上方显示按键的字如 ...
- scss-@mixin传参
混合器一个很重要特性就是可以传递参数,可以根据不同场景来定制css代码的复用.极大提高了混合器的适用性,看如下scss代码实例: @mixin makeradius($radius) { border ...
- javascript实现数据结构:稀疏矩阵的十字链表存储表示
当矩阵的非零个数和位置在操作过程中变化大时,就不宜采用顺序存储结构来表示三元组的线性表.例如,在作“将矩阵B加到矩阵A上”的操作时,由于非零元的插入或删除将会引起A.data中元素的移动.为此,对这种 ...
- python str、int、dict
一.str print(dir(int))#['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', ...
- Spring MVC 如何加载静态html
在spring mvc的xml文件最后面加上下面这一行<mvc:deault-servlet-handler/>
- linux常用指令集-持续更新...
0.查看所有java进程GC情况:for i in `jps|egrep -v "Jps|Launcher" |cut -d" " -f1`;do pwdx $ ...