美文网首页
(八)文件

(八)文件

作者: 费云帆 | 来源:发表于2018-12-29 14:17 被阅读0次

    1.读取文件的操作---open()

    """
    使用cmd进入文件目录,输入'python',回车进入交互模式
    """
    # 创建一个file对象
    >>> file=open('test.txt')
    # 这里没用read(),而是用for去遍历
    >>> for line in file:
    ...     print(line)
    ...
    #多余的换行是由于print()引起的.
    1.This is the first test line.
    
    2.This is the second test line.
    
    3.This is the third test line.
    # 这里如果继续循环,因为已经到了末尾,故什么都没有!
    >>> for line in file:
    ...     print(line,end='')
    ...
    # 重新读取,需重新赋值(由于知识面的缺乏,暂时这么理解)
    >>>
    >>> file1=open('test.txt')
    >>> for line in file1:
    ...     print(line,end='')
    ...
    # print()的换行消失了.
    1.This is the first test line.
    2.This is the second test line.
    3.This is the third test line.
    

    2.创建文件的基本操作:

    # 会出现一个空白的test1.txt文档
    >>> file2=open('test1.txt','w')
    # 执行到下面这句,文档依然空白,但是返回文档的长度
    >>> file2.write('This is just for test2!!!')
    25
    # 关闭了文件后,文件才正式有内容在里面
    >>> file2.close()
    # review版,上述相当于以下代码
    with open('E:/Bin/Python/test/test4.txt','w') as file:
        file.write('This is a review example!!!')
    

    3.读取文件的状态---os,time模块

    >>> import os
    >>> file_stat=os.stat("test.txt")
    >>> file_stat
    os.stat_result(st_mode=33206, st_ino=3096224743817860, st_dev=150876, st_nlink=1
    , st_uid=0, st_gid=0, st_size=97, st_atime=1546063630, st_mtime=1546063705, st_c
    time=1546063630)
    # 看不懂时间格式,使用time模块解析
    >>> file_stat.st_ctime
    1546063630.975547
    >>> import time
    >>> time.localtime(file_stat.st_ctime)
    time.struct_time(tm_year=2018, tm_mon=12, tm_mday=29, tm_hour=14, tm_min=7, tm_s
    ec=10, tm_wday=5, tm_yday=363, tm_isdst=0)
    

    4.read(),readline(),readlines()

    >>>file3=open('you.md')
    # 输出全部的内容,包含换行符,是字符串对象
    >>> file3.read()
    'You Raise Me Up\nWhen I am down and, oh my soul, so weary;\nWhen troubles come
    and my heart burdened be;\nThen, I am still and wait here in the silence,\nUntil
     you come and sit awhile with me.\nYou raise me up, so I can stand on mountains;
    \nYou raise me up, to walk on stormy seas;\nI am strong, when I am on your shoul
    ders;\nYou raise me up: To more than I can be.'
    >>> file4=open('you.md')
    # 逐行输出
    >>> file4.readline()
    # 逐行输出
    'You Raise Me Up\n'
    # 逐行输出
    >>> file4.readline()
    'When I am down and, oh my soul, so weary;\n'
    >>> file4.readline()
    'When troubles come and my heart burdened be;\n'
    >>> file4.readline()
    'Then, I am still and wait here in the silence,\n'
    >>> file5=open('you.md')
    # 全部输出,但是结果是一个list对象
    >>> file5.readlines()
    ['You Raise Me Up\n', 'When I am down and, oh my soul, so weary;\n', 'When troub
    les come and my heart burdened be;\n', 'Then, I am still and wait here in the si
    lence,\n', 'Until you come and sit awhile with me.\n', 'You raise me up, so I ca
    n stand on mountains;\n', 'You raise me up, to walk on stormy seas;\n', 'I am st
    rong, when I am on your shoulders;\n', 'You raise me up: To more than I can be.'
    ]
    
    • 当文件很大的时候,如果还是用上述方法,内存可能会崩...这时还是使用for循环去遍历比较妥当,当然,还有另一种推荐的方法---fileinput模块:
    import fileinput
    # 创建一个fileinput对象
    # 我们之前是创建一个open对象
    file=fileinput.input('you.md')
    for line in file:
        print(line,end='')
    >>>
    You Raise Me Up
    When I am down and, oh my soul, so weary;
    When troubles come and my heart burdened be;
    Then, I am still and wait here in the silence,
    Until you come and sit awhile with me.
    You raise me up, so I can stand on mountains;
    You raise me up, to walk on stormy seas;
    I am strong, when I am on your shoulders;
    You raise me up: To more than I can be.
    

    5.seek---"指针"的跳转

    >>> file=open('you.md')
    >>> file.readline()
    'You Raise Me Up\n'
    >>> file.readline()
    'When I am down and, oh my soul, so weary;\n'
    #跳回起始的文件位置
    >>> file.seek(0)
    0
    >>> file.readline()
    'You Raise Me Up\n'
    #从0开始算起
    >>> file.tell()
    17
    

    相关文章

      网友评论

          本文标题:(八)文件

          本文链接:https://www.haomeiwen.com/subject/apnulqtx.html