一、打开文件的方法:

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-文件基本操作(一)的更多相关文章

  1. [ Python入门教程 ] Python文件基本操作

    本文将python文件操作实例进行整理,以便后续取用. 文件打开和创建 Python中使用open()函数打开或创建文件.open()的声明如下: open(name[, mode[, bufferi ...

  2. Python文件基本操作及上下文管理

    文件基本操作 打开文件:f = open(fole_name,mode = 'r'),传入表示文件路径的字符串,会返回一个文件对象,mode是文件打开模式. 关闭文件:f.close(),调用给定文件 ...

  3. python文件基本操作(读,写,追加)

    一:只读(r) f=('d:\ python的联系文件'')   绝对路径和相对路径(绝对路径:能找到文件开始到结束路径,真实存在的路径,相对路径:在绝对路径一致的情况下新建一个文件) f=open( ...

  4. [ Python入门教程 ] Python文件基本操作_shutil模块

    shutil模块是对os模块中文件操作的补充,提供文件和目录的移动.复制.打包.压缩.解压等功能 shutil常用函数   shutil.copyfile(src, dst)   复制文件, 如果ds ...

  5. python文件(概念、基本操作、常用操作、文本文件的编码方式)

    文件 目标 文件的概念 文件的基本操作 文件/文件夹的常用操作 文本文件的编码方式 01. 文件的概念 1.1 文件的概念和作用 计算机的 文件,就是存储在某种 长期储存设备 上的一段 数据 长期存储 ...

  6. python学习9—文件基本操作与高级操作

    python学习9—文件基本操作与高级操作 1. 文件基本操作 打开文件,获得文件句柄:f = open('filename',encoding='utf-8'),open会查询操作系统的编码方式,并 ...

  7. 从零开始的Python学习Episode 7——文件基本操作

    文件基本操作 一.打开文件 f = open('11','r')#open('file path','mode') 创建一个文件对象 文件有多种打开模式: 1. 'r':新建一个文件对象以只读方式打开 ...

  8. python文件I/O(转)

    Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你 ...

  9. python 文件操作总结

    Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你 ...

  10. Python基础篇【第2篇】: Python文件操作

    Python文件操作 在Python中一个文件,就是一个操作对象,通过不同属性即可对文件进行各种操作.Python中提供了许多的内置函数和方法能够对文件进行基本操作. Python对文件的操作概括来说 ...

随机推荐

  1. Android应用程序组件之间的通信Intent和IntentFilter

    Android应用程序的基本组件,这些基本组建除了Content Provider之外,几乎全部都是依靠Intent对象来激活和通信的. 下面介绍Intent类,并通过例子来说明Intent一般用法 ...

  2. HDU 4171 Paper Route

    Problem Description As a poor, tuition-ridden student, you've decided to take up a part time job as ...

  3. indexOf.substr,substring,charAt的区别

    var a = "asdfghjkl" alert(a.substr(1, 3));        //  从下标为1开始,往右数3个长度的数, 显示 sdf; alert(a.s ...

  4. asp 2.0 ajax triggers 触发更新

  5. Hibernate课堂笔记

    1.java持久化概述 Java持久化简称(JPA), 即把程序中的临时数据持久保存到数据库中.由于jdbc开发效率低,我们就提出了对象关系映射(ORM)的概率 2.ORM 通过java持久化提供的A ...

  6. main方法击破

    什么是main方法? 是类中的一段代码,可以让程序独立运行. public class HelloWord{ public static void main(String[] args) { for ...

  7. ArcGIS 10.3编译旧版本Addin错误的解决办法

    ArcGIS10.2下VS2010的AddIn,在10.3下在VS2012下重新编译出现missing ESRI ArcGIS Add-in SDK错误,导致无法生成esriAddIn安装文件. 该问 ...

  8. postman接口案例

    接口测试 什么是接口(API) API全称Application Programming Interface,这里面我们其实不用去关注AP,只需要I上就可以.一个API就是一个Interface.我们 ...

  9. asyncio标准库4 asyncio performance

    性能包括2部分 每秒并发请求数(Number of concurrent requests per second) 每秒请求负载(Request latency in seconds: min/ave ...

  10. sqlserver学习2---java执行存储过程

    一.存储过程 1.新增操作存储过程 --------------1.新建 增加学生的存储过程---------------------------- set IDENTITY_INSERT stude ...