Python文件IO
Python文件IO
有如下文本内容,文件路径为D:\temp,文件名称为lyric.txt,
line1 Look !
line2 If U had one shot
line3 One opportunity
line4 To seize everything U ever wanted
line5 One moment
line6 Would U capture it ?
line7 Or just let it slip
- 逐行读取,并输出
#coding=utf-8
import os
file_path = r'D:\temp'
file_name = 'lyric.txt'
#拼接文件路径与名称
file_URI = os.path.join(file_path,file_name)
print("file_URI-- " + file_URI)
fd = open(file_URI, mode='r')
#逐行读取文件内容
for line in fd:
#输出每行内容,每行行尾有换行符号
print(line)输出结果,单独输出每行,包含此行的换行符:
file_URI-- D:\temp\lyric.txt
line1 Look ! line2 If U had one shot line3 One opportunity line4 To seize everything U ever wanted line5 One moment line6 Would U capture it ? line7 Or just let it slip- read(),读取全部内容
#coding=utf-8
import os
file_path = r'D:\temp'
file_name = 'lyric.txt'
file_URI = os.path.join(file_path,file_name)
print("file_URI-- " + file_URI)
fd = open(file_URI, mode='r')
content = fd.read()
print(content)输出结果
file_URI-- D:\temp\lyric.txt
line1 Look !
line2 If U had one shot
line3 One opportunity
line4 To seize everything U ever wanted
line5 One moment
line6 Would U capture it ?
line7 Or just let it slip - readlines(),读取全部内容,返回每行内容作为元素的列表
#coding=utf-8
import os
file_path = r'D:\temp'
file_name = 'lyric.txt'
file_URI = os.path.join(file_path,file_name)
print("file_URI-- " + file_URI)
fd = open(file_URI, mode='r')
content_list = fd.readlines()
print(content_list)输出结果
file_URI-- D:\temp\lyric.txt
['line1 Look ! \n', 'line2 If U had one shot\n', 'line3 One opportunity\n', 'line4 To seize everything U ever wanted\n', 'line5 One moment\n', 'line6 Would U capture it ? \n', 'line7 Or just let it slip']
Python文件IO的更多相关文章
- python 文件 IO 操作
Python 的底层操作 * 其实Python的文件IO操作方法,和Linux底层的差不多 打开 f = open(filename , "r") 后面的 "r" ...
- Python 文件IO
Python 文件I/O 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你传递的表达式转换成一个字符串表达式,并将结果写到标准输出如下: #!/u ...
- Python文件IO(普通文件读写)
## 打开一个文件 - fileobj = open(filename, mode) 其中: fileobj是open()返回的文件对象 filename是该文件的字符串名 mode是指明文件类型和操 ...
- 老男孩python学习自修【第一天】文件IO用法
第一天 文件IO处理 1.读文件实例 file_split.python f = file('myFile.txt', 'r') for line in f.readlines(): line = ...
- Python文件基础操作(IO入门1)
转载请标明出处: http://www.cnblogs.com/why168888/p/6422270.html 本文出自:[Edwin博客园] Python文件基础操作(IO入门1) 1. pyth ...
- python学习之路-第八天-文件IO、储存器模块
文件IO.储存器模块 文件IO 代码示例: # -*- coding:utf-8 -*- #! /usr/bin/python # filename:using_file.py poem = '''\ ...
- Python学习(15)文件/IO
目录 Python 文件I/O 打印到屏幕 读取键盘输入 打开和关闭文件 File对象属性 文件定位 重命名和删除文件 Python的目录 Python 文件I/O 本章只讲述所有基本的的I/O函数, ...
- Python进阶4---Python的文件IO
文件操作 体存储单元,包括随机存储器(RAM),只读存储器(ROM),以及高速缓存(CACHE).只不过因为RAM是其中最重要的存储器. 通常所说的内存即指电脑系统中的RAM.RAM要求每时每刻都不 ...
- Python文件基础
===========Python文件基础========= 写,先写在了IO buffer了,所以要及时保存 关闭.关闭会自动保存. file.close() 读取全部文件内容用read,读取一行用 ...
随机推荐
- Linux服务器集群系统(一)(转)
add by zhj:虽然是2002年的文章,但读来还是收益良多.在 章文嵩:谈LVS及阿里开源背后的精彩故事 中LVS发起人及主要贡献者谈了LVS的开发过程及阿里开源的一些故事 原文:http:// ...
- Web Service学习之八:Soap消息详解
一.区别概念 WSDL是网络服务描述语言,是XML文档:它包含一系列描述某个web service的定义或者说是规则.尤其是定义了传输Sope消息的结构 Soap:简单对象访问协议,是交换数据的一种协 ...
- JSF 2 textarea example
In JSF, you can use the <h:inputTextarea /> tag to render a HTML textarea field. For example, ...
- mybatis generator配置文件
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration ...
- MES系统的有用存储过程
USE [ChiefmesNEW]GO/****** Object: StoredProcedure [dbo].[st_WMS_ImportStockInBill] Script Date: 10/ ...
- status pending状态
开发采用ssh,注解的方式,事物也application.xml配置了,但是在业务层没有使用@Transactional造成浏览器一直处于status pending状态,为什么没有使用@Transa ...
- python函数介绍
1.向函数传递信息 def greet_user(username): print('Hello,' + username.title() + '!') greet_user('jesse') 2.位 ...
- Android ViewPager使用具体解释
这是谷歌官方给我们提供的一个兼容低版本号安卓设备的软件包,里面包囊了仅仅有在安卓3.0以上能够使用的api.而viewpager就是当中之中的一个利用它,我们能够做非常多事情,从最简单的导航,到页面菜 ...
- TFS 图标意思
小人:文件被其他人签出 对号:文件被自己签出 锁: 文件已经被签入 加号:新增加一个文件
- delphi 2010 资源文件使用
Project Recources... //1提取出资源 procedure TForm1.Button1Click(Sender: TObject);begin w ...