zoukankan      html  css  js  c++  java
  • Python----文件操作

    内建函数Open(name,mode,buffering)语法结构理解:

    1.name:文件路径名称

    2.mode:文件打开模式

     

    3.buffering:

    用于指示访问文件所采用的缓存方式。0表示不缓存;1表示只缓存一行,n代表缓存n行。如果不提供或为负数,则代表使用系统默认的缓存机制

    例如:

    (1)以只读方式打开Windows环境下.txt文件:

     

    1、如下图E盘下文件test.txt;

    2、在IDLE (Python GUI)中执行:

    Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32

    Type "copyright", "credits" or "license()" for more information.

    >>> f = open("e:/test.txt","r")   #R是以只读的方式打开,文件必须已经存在

    >>> print(f.read())

    ShinSangokumusou6

    ShinSangokumusou7

    ShinSangokumusou8

    ShinSangokumusou9

    ShinSangokumusou10

    >>> f.close()

    >>>

    (2)以写入方式打开Windows环境下.txt文件:

    >>> f = open("e:/test.txt","w")    #W是以写入的方式打开,文件若存在则先清空,后重建

    >>> print(f)

    <open file 'e:/test.txt', mode 'w' at 0x02A0BA70>

    >>> f.write(raw_input("Type what you want:"))

    Type what you want:PYTHON is wonderful

    >>> f.close()

    >>> f = open("e:/test.txt","r")

    >>> print(f.read())

    PYTHON is wonderful

    >>>

     

    Close()

    >>> f.closed       #判断是否关闭

    False

    >>> f.close()       #关闭方法

    >>> f.closed

    True

    >>> f.tell()          #如果已关闭,则一切针对文件操作都无效

    Traceback (most recent call last):

      File "<pyshell#27>", line 1, in <module>

        f.tell()

    ValueError: I/O operation on closed file

    >>>

    内建方法:

    >>> dir(f)

    ['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'closed', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines']

    >>> 

    生命不息,折腾不止;不计后果,不问前程!
  • 相关阅读:
    PMP:7.项目成本管理
    PMP:5.项目范围管理
    PMP:4.项目整合管理
    PMP:3.项目经理角色
    PMP:2.项目运行环境
    PMP:1.引论
    RxSwift学习笔记10:startWith/merge/zip/combineLatest/withLatestFrom/switchLatest
    RxSwift学习笔记9:amb/tabkeWhile/tabkeUntil/skipWhile/skipUntil
    RxSwift学习笔记8:filter/distinctUntilChanged/single/elementAt/ignoreElements/take/takeLast/skip/sample/debounce
    linux 安装rz 指令
  • 原文地址:https://www.cnblogs.com/jionjionyou/p/5475443.html
Copyright © 2011-2022 走看看