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

  

  1. 逐行读取,并输出

    #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)

    输出结果,单独输出每行,包含此行的换行符:

  2. 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
  3. 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
  4. 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的更多相关文章

  1. python 文件 IO 操作

    Python 的底层操作 * 其实Python的文件IO操作方法,和Linux底层的差不多 打开 f = open(filename , "r") 后面的 "r" ...

  2. Python 文件IO

    Python 文件I/O 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你传递的表达式转换成一个字符串表达式,并将结果写到标准输出如下: #!/u ...

  3. Python文件IO(普通文件读写)

    ## 打开一个文件 - fileobj = open(filename, mode) 其中: fileobj是open()返回的文件对象 filename是该文件的字符串名 mode是指明文件类型和操 ...

  4. 老男孩python学习自修【第一天】文件IO用法

    第一天   文件IO处理 1.读文件实例 file_split.python f = file('myFile.txt', 'r') for line in f.readlines(): line = ...

  5. Python文件基础操作(IO入门1)

    转载请标明出处: http://www.cnblogs.com/why168888/p/6422270.html 本文出自:[Edwin博客园] Python文件基础操作(IO入门1) 1. pyth ...

  6. python学习之路-第八天-文件IO、储存器模块

    文件IO.储存器模块 文件IO 代码示例: # -*- coding:utf-8 -*- #! /usr/bin/python # filename:using_file.py poem = '''\ ...

  7. Python学习(15)文件/IO

    目录 Python 文件I/O 打印到屏幕 读取键盘输入 打开和关闭文件 File对象属性 文件定位 重命名和删除文件 Python的目录 Python 文件I/O 本章只讲述所有基本的的I/O函数, ...

  8. Python进阶4---Python的文件IO

     文件操作 体存储单元,包括随机存储器(RAM),只读存储器(ROM),以及高速缓存(CACHE).只不过因为RAM是其中最重要的存储器. 通常所说的内存即指电脑系统中的RAM.RAM要求每时每刻都不 ...

  9. Python文件基础

    ===========Python文件基础========= 写,先写在了IO buffer了,所以要及时保存 关闭.关闭会自动保存. file.close() 读取全部文件内容用read,读取一行用 ...

随机推荐

  1. 我是怎么发现并解决项目页面渲染效率问题的(IE调试工具探查器的使用)

    #我是怎么发现并解决项目页面渲染效率问题的(IE调试工具探查器的使用) ##背景 之前的项目中,有很多的登记页面,一般都有100-200甚至更加多的字段,而且还涉及到字典.日期及其他效果的显示,载入时 ...

  2. Type datetime2 is not a defined system type - Entity Framework 摘自网络

    "Type datetime2 is not a defined system type" Solution: 把edmx 改为 ProviderManifestToken=&qu ...

  3. Apache Spark MLlib的简介

    MLlib 是构建在 Spark 上的分布式机器学习库,充分利用了 Spark 的内存计算和适合迭代型计算的优势,将性能大幅度提升.同时由于 Spark 算子丰富的表现力, 让大规模机器学习的算法开发 ...

  4. jvm所占空间的配置

    http://www.cnblogs.com/mingforyou/archive/2012/03/03/2378143.html

  5. ACM OJ Collection

    浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 同济大学(TJU):http://acm.t ...

  6. POJ 1258 Agri-Net (最小生成树)

    Agri-Net 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/H Description Farmer John has be ...

  7. HDU 1455 http://acm.hdu.edu.cn/showproblem.php?pid=1455

    #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #de ...

  8. thymeleaf的url属性

    一.使用表达式形式:@{...} 例如: <a th:href="@{http://localhost:8080/gtvg/order/details}">view&l ...

  9. C# 索引器 学习

    转载原地址: http://www.cnblogs.com/lxblog/p/3940261.html 1.索引器(Indexer): 索引器允许类或者结构的实例按照与数组相同的方式进行索引.索引器类 ...

  10. Web开源框架大汇总

    Struts 项目简介信息 Struts是一个基于Sun J2EE平台的MVC框架,主要是采用Servlet和JSP技术来实现的.由于Struts能充分满足应用开发的需求,简单易用,敏捷迅速,在过去的 ...