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对文件的操作概括来说 ...
随机推荐
- sudo apt-get install openssh-server时提示需要安装1:6.6p1-2ubuntu1的解决办法(图文详解)
最近,在执行 sudo apt-get install openssh-server 提示,如下. 解决办法 先要执行 sudo apt-get install openssh-client=1:6. ...
- 【JAVA】重载和重写的区别
重写(Overriding) 重写规则 1. 参数列表:必须与被重写方法的参数列表完全匹配. 2. 返回类型:必须与超类中被重写的方法中声明的返回类型或子类型完全相同 3. 访问级别:一定不能比被 ...
- GoLang爬取花瓣网美女图片
由于之前一直想爬取花瓣网(http://huaban.com/partner/uc/aimeinv/pins/) 的图片,又迫于没时间,所以拖了很久. 鉴于最近在学go语言,就刚好用这个练手了. 预览 ...
- 2.3 js基础--DOM
一.javascript组成 ECMAScript:核心解释器[为我们提供好了最基本的功能:变量声明.函数.语法.运算]. 兼容性:完全兼容. DoM:文档对象 ...
- HDU 1069—— Monkey and Banana——————【dp】
Monkey and Banana Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- [转]使用 Razor 进行递归操作
本文转自:http://www.cnblogs.com/zbw911/archive/2013/01/10/2855025.html 做一个菜单,多级的会遇到递归的问题,打算在code中做一个递归方法 ...
- rabbitmq 命令&& rabbitmq教程(一)
先来个官方教程 http://www.rabbitmq.com 在windows 下 命名 去掉sudo 我是在windows下测试 用net调用 常用命令 控制台命令:sudo rabbitmqct ...
- jsp的动作标签
常用的标签: 1. forward 请求转发 [基本不使用] <==> request.getRequestDispatcher(url).forward(request,respon ...
- sql使用自连接去重复查询
查询公司与公司最新刷新的一条信息 select t1.userid,t1.id,t1.title,t1.RegType,t1.Salary,t1.SubjectID,t1.RefreshTime,t2 ...
- 关于JVM
Java 中通过多线程机制使得多个任务同时执行处理,所有的线程共享JVM内存区域main memory,而每个线程又单独的有自己的工作内存,当线程与内存区域进行交互时,数据从主存拷贝到工作内存,进而交 ...