Python file operations:

Source: Internet
Author: User

The open function , which is used for file processing

When working with files, you typically need to go through the following steps:

    • Open File
    • Manipulating files

First, open the file

1 文件句柄 =open(‘文件路径‘, ‘模式‘)

When you open a file, you need to specify the file path and how you want to open the file, and then open it to get the file handle and manipulate it later through the file handle.

The mode of opening the file is:

    • R, read-only mode (default).
    • W, write-only mode. "unreadable; not exist; create; delete content;"
    • A, append mode. "Readable; not exist" create; "only append content;"

"+" means you can read and write a file at the same time

    • r+, can read and write files. "readable; writable; can be added"
    • w+, write and read
    • A +, with a

"U" means that the \ r \ n \ r \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ r or r+ mode can be converted automatically when reading

    • RU
    • R+u

"B" means processing binary files (e.g. FTP send upload ISO image file, Linux can be ignored, Windows process binary file should be labeled)

  • Rb
  • Wb
  • Ab
  • Second, the operation
  • Python 2.x
    ClassTextiowrapper (_textiobase):"""    Character and line based layer over a Bufferediobase object, buffer. Encoding gives the name of the encoding that the stream would be a decoded or encoded with.        It defaults to Locale.getpreferredencoding (False). Errors determines the strictness of encoding and decoding (see Help (Codecs.        CODEC) or the documentation for Codecs.register) and defaults to "strict". NewLine controls how line endings is handled.  It can be None, ', ' \ n ', ' \ R ', and ' \ r \ n '. It works as follows: * on input, if newline are None, universal newlines mode is enabled.      Lines in the input can end in ' \ n ', ' \ R ', or ' \ r \ n ', and these is translated into ' \ n ' before being returned to the Caller. If it is ", universal newline mode is enabled, but line endings was returned to the caller untranslated. If it has any of the other legal values, input lines is only terminated by the given string, and the line Endin  G is returned to the caller untranslated.      * On output, if newline was None, any ' \ n ' characters written is translated to the system default line Separato R, Os.linesep. If newline is ' or ' \ n ', no translation takes place.        If NewLine is any of the other legal values, any ' \ n ' characters written is translated to the given string.     If line_buffering is True, a call to flush was implied when a call to write contains a newline character."""def close (self, *args, **kwargs):#Real signature UnknownClose FilePassdef fileno (self, *args, **kwargs):#Real signature UnknownFile descriptorPassdef flush (self, *args, **kwargs):#Real signature UnknownFlush file Internal BuffersPassdef isatty (self, *args, **kwargs):#Real signature UnknownDetermine if the file is a consent TTY devicePassdef read (self, *args, **kwargs):#Real signature UnknownReads the specified byte dataPassdef readable (self, *args, **kwargs):#Real signature UnknownWhether it is readablePassdef readline (self, *args, **kwargs):#Real signature UnknownRead only one row of dataPassdef seek (self, *args, **kwargs):#Real signature UnknownSpecify pointer position in filePassdef seekable (self, *args, **kwargs):#Real signature UnknownWhether the pointer can be manipulatedPassdef tell (self, *args, **kwargs):#Real signature UnknownGet pointer positionPassdef truncate (self, *args, **kwargs):#Real signature UnknownTruncate data, preserving only the previous data specifiedPassdef writable (self, *args, **kwargs):#Real signature UnknownWhether it can be writtenPassDef write (self, *args, **kwargs):#Real signature UnknownWrite contentPassDef__getstate__ (self, *args, **kwargs):#Real signature UnknownPassDef__init__ (self, *args, **kwargs):#Real signature UnknownPass@staticmethod#Known case of __new__Def__new__ (*args, **kwargs):#Real signature Unknown"""Create and return a new object. See Help (type) for accurate signature."""PassDef__next__ (self, *args, **kwargs):#Real signature Unknown"""Implement Next (self)."""PassDef__repr__ (self, *args, **kwargs):#Real signature Unknown"""Return repr (self)."""PassBuffer = Property (Lambda Self:object (),Lambda Self, V:none,Lambda Self:none)#DefaultClosed = Property (Lambda Self:object (),Lambda Self, V:none,Lambda Self:none)#DefaultEncoding = Property (Lambda Self:object (),Lambda Self, V:none,Lambda Self:none)#DefaultErrors = Property (Lambda Self:object (),Lambda Self, V:none,Lambda Self:none)#DefaultLine_buffering = Property (Lambda Self:object (),Lambda Self, V:none,Lambda Self:none)#DefaultName = Property (Lambda Self:object (),Lambda Self, V:none,Lambda Self:none)# Default Newlines = Property ( lambda self:object (), lambda self, V:none, lambda self:none) # default  _chunk_size = Property (lambda self:object (), lambda self, v:none, lambda self:none) # Defaultlambda self:object (), Span style= "color: #0000ff;" >lambda self, v:none, lambda self:none) # default                

Python file operation:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.