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. 开源框架DNN使用01

    我先简单地介绍下我个人对于DNN的浅显理解吧. 我觉得对于刚接触的人来说首先理解DNN的原理,大框架是很重要的.它整个网站其实是没几个页面的,从源码上就可以看出, 一个Default页.一个Error ...

  2. [HIve - LanguageManual] LateralView

    Lateral View Syntax Description Example Multiple Lateral Views Outer Lateral Views Lateral View Synt ...

  3. Language Basics:语言基础

    Java包含多种变量类型:Instance Variables (Non-Static Fields)(实例变量):是每个对象特有的,可以用来区分各个实例Class Variables (Static ...

  4. Lucene Query Term Weighting

    方法 public static Query TermWeighting(Query tquery,Map<String,Float>term2weight){ BooleanQuery ...

  5. 《Java数据结构与算法》笔记-CH1

    /** * 数据结构的特性 ************************************************************************************** ...

  6. lipo命令

    工作中,xcode工程遇到一个bug file was built for archive which is not the architecture being linked armv7 找了一些资 ...

  7. Gym 100507L Donald is a postman (水题)

    Donald is a postman 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/L Description Donald ...

  8. 【转】强大的vim配置文件,让编程更随意

    原文地址:http://www.cnblogs.com/ma6174/archive/2011/12/10/2283393.html 花了很长时间整理的,感觉用起来很方便,共享一下. 我的vim配置主 ...

  9. C 语言中包含的标准头文件(24个)

    <assert.h><complex.h><ctype.h><errno.h><fenv.h><float.h><intt ...

  10. HDU 1856 More is better(并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others) ...