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对文件的操作概括来说 ...
随机推荐
- cxf 框架 webservice
cxf 内置了一个web服务器 cxf简单入门实例 package test; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import c ...
- oracle trim不掉空白字符分享(转)
本文转自:http://www.2cto.com/database/201306/223558.html 问题背景:一个工商注册号,正常的用trim能解决的问题,但是这个case,trim后和肉眼看到 ...
- 1、java线程模型
要了解多线程,先需要把java线程模型搞清楚,否则有时候很难理清楚一个问题. 硬件多线程: 物理机硬件的并发问题跟jvm中的情况有不少相似之处,物理机的并发处理方案对于虚拟机也有相当大的参考意义.在买 ...
- pat1006. Sign In and Sign Out (25)
1006. Sign In and Sign Out (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- "Sorry this application cannot run under a virtual machine" Error
错误: 运行一个程序是出现了 “sorry this application cannot run under a virtual machine” 错误. 如何解决: 控制面板-->卸载程序- ...
- springboot整合activeMq 跳坑
安装 activeMq 安装请看我的另一篇https://www.cnblogs.com/milicool/p/8420926.html 版本 springboot 2.0.5.RELEASE 项目结 ...
- Java原生隐藏字符-工具类
package com.seesun2012.common.util; /** 隐藏字符-工具类 @author seesun2012@163.com */ public class HiddenCh ...
- Windows server 搭建ftp服务器
1.安装ftp 2.端口端口21和20的入出端口 3.点击IIS 服务器证书 4.FTP界面上选择“FTP身份验证”——>“基本身份验证”–>”启用” 5.FTP界面选择 “FTP授权规则 ...
- vs文件属性(复制到输出目录)是什么意思
右击项目里的文件,选择属性(F4)会有复制到输出目录的选项. 它提供三项选择,如图: 如果选择始终复制或如果较新则复制会在该程序集的bin目录下生成该文件,如图:
- package.json中dependencies 和devDependencies的差异
我们在日常开发中,经常会使用到npm安装对应的包,会经常在package.json中看到dependencies 和devDependencies 二者的区别: devDependencies:是你开 ...