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,读取一行用 ...
随机推荐
- c函数习记
1,user groups 篇幅 the length of an article; fgetgrent(从指定的文件来读取组格式) 相关related functions;fgetpwent hea ...
- 第二百七十八天 how can I 坚持
生命的意义.必须要做点什么啊.今年我们二十七八岁. 遇事不急,理清头绪就没那么复杂. 今天突然有点悔意,元旦好像应该不回去看房,花销有点大了,算了,过去的就让他过去吧,都是回忆.至少玩的挺嗨. 记住, ...
- 第二百六十天 how can I 坚持
晚上去看了个电影,<万万没想到>,挺好看的,豆瓣评分不高.网络啊. 互联网会让聪明的人越来越聪明. 明天去看寻龙诀了. 懒惰会生根发芽,哈哈. 睡觉了.
- 【Linux常用工具】02. 创建启动定时任务工具cron
一. cron 1. cron是一个守护程序,它提供定时器的功能,让用户在特定的时间得以执行默认的指令或程序.只要用户会编辑定时器的设置文件,就可以使用定时器的功能. 定时器文件格式: 2. cron ...
- 《数据通信与网络》笔记--SCTP
SCTP(stream control transmission protocol)是一种新的可靠的,面向报文的传输层控制协议.它兼有UDP和TCP的特性,它是可靠的面向报文的协议,它保存报文的边界, ...
- CodeForces 706B Interesting drink (二分查找)
题意:给定 n 个数,然后有 m 个询问,每个询问一个数,问你小于等于这个数的数有多少个. 析:其实很简单么,先排序,然后十分查找,so easy. 代码如下: #pragma comment(lin ...
- pymol编译
https://pymolwiki.org/index.php/Linux_Install
- iOS开发笔记系列-基础5(分类和协议)
分类 在Objective-C中,除了通过新建子类的方式来向类添加新方法外,还可以通过分类的方式.分类提供了一种简单的方式,将类的定义模块化到相关方法的组或分类中,它还提供了扩展现有类定义的简便方式, ...
- Codeforces Round #328 (Div. 2) B. The Monster and the Squirrel 打表数学
B. The Monster and the Squirrel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/c ...
- TC SRM 665 DIV2 B LuckyCycle 暴力
LuckyCycleTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.ac ...