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对文件的操作概括来说 ...
随机推荐
- 里氏替换原则(Liskov Substitution Principle) LSP
using System; using System.Collections.Generic; using System.Text; namespace LiskovSubstitutionPrinc ...
- 写些最近两个学安卓的笔记-关于Toast
1.Toast可以在Activity和service里使用,在Service里使用时,Toast是显示在当前的Activity上. 2.Toast出现时,当前的Activity依然可见可交互. 3.T ...
- Unity3D 发布成PC端常用设置
本文,基于Unity 5.6pro版本来发布PC端.文中若有不妥之处,欢迎各位指出! 一.如何去掉Unity官方水印? 首先,你需要pro版本的Unity3D.如果,你是personal版本的话,就需 ...
- Python 集合(set)类型的操作——并交差
介绍 python的set是一个无序不重复元素集,基本功能包括关系测试和消除重复元素. 集合对象还支持并.交.差.对称差等. sets 支持 x in set. len(set).和 for x in ...
- ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Subm ...
- git本地创建新分支并推送到远程仓库
1,在当前项目目录,从已有的分支创建新的分支(如从master分支),创建一个dev分支 git checkout -b dev 2,创建完可以查看一下,分支已经切换到dev git branch * ...
- [转]Asp.net Core 使用Redis存储Session
本文转自:http://www.cnblogs.com/hantianwei/p/5723959.html 前言 Asp.net Core 改变了之前的封闭,现在开源且开放,下面我们来用Redis存储 ...
- c# 远程连接共享文件
c# 远程连接共享文件 /// <summary> /// 连接远程共享文件夹 /// </summary> /// <param name="path&quo ...
- Spring注解之Controller中获取请求参数及验证使用
1.处理request的uri部分的参数:@PathVariable. 2.处理request header部分的参数:@RequestHeader,@CookieValue@RequestHeade ...
- Django分页解析
分页 django中实现管理数据分页的类位于 django.core.paginator.py中 Paginator类 对列表数据进行分页处理 对象 Paginator(Post.objects.al ...