Python core programming learning diary file and Input and Output

Source: Internet
Author: User
File and input/output
  1. Object
    1. Generic file objects: objects with file-type interfaces, such as files, Web pages, and communications. A file is a continuous byte sequence. Byte streams are often used for data transmission.
    2. Open (): file_object = open (file_name, access_mode = 'R', buffering =-1 ). File_name can be a considerable path or an absolute path. File () and open () functions can be completely replaced.
    3. General line breaks (UNS): When the 'U' flag is used to open a file, all row delimiters are replaced with NEWLINE (\ n) when returned through the Python input method ). The file object has the newlines attribute. UNS is only used to read text files, and there is no corresponding method to process file output.
    4. Discussions about binary and text files are at the end of the Notes
  2. File built-in method

    File methods include input, output, file movement, and miscellaneous operations.

    1. Input:

      The read () method is used to directly read size bytes into strings. No size is specified. read to the end of the file. (@ Deprecated)

      The readline () method reads a line that opens the file, and returns the entire line that contains the row Terminator as a string. The size parameter is also available.

      The readlines () method reads all (remaining) rows and returns the string list. Sizhint indicates the maximum size of the returned bytes.

    2. Output:

      Write () writes a string containing text data or binary data blocks to a file.

      Writelines () writes a string list to a file, but the row Terminator is not automatically added to the end of the row. You need to process the strings in the list by yourself

      Read () and readlines () read rows. Row delimiters are not automatically deleted, and write () and writelines () are not added to the row delimiters. You must perform operations on your own.

    3. File movement:

      Seek () can move the file pointer to different locations in the file. Offset bytes indicate the offset relative to a certain position. The default position is 0, which indicates starting from the beginning of the file, 1 indicates starting from the current position, and 2 indicates starting from the end of the file. (Similar to fseek ()).

      The text () method indicates the position of the current file pointer in the file, measured in bytes starting from the file.

    4. Miscellaneous file methods:

      Close (): close the file to end its access. Releasing a handle as early as possible is a good habit to avoid idle usage of resources. Close file is not displayed, and data in the output buffer may be lost.

      Fileno () returns the descriptor of the opened file. The flush () method directly writes data in the internal buffer to the file immediately. The truncate () method truncate the file to the current file pointer location or to the given size, in bytes.

      The line separator is different from other file systems: On Posix systems, the line separator is the NEWLINE ('\ n') character, and the old MacOS is the RETURN (\ r) character ), DOS and Wind32 systems use both (\ r \ n ). The path separator Posix uses "/", Dos and Windows uses "\", and the old MacOS uses ":". When the OS module is imported, variables with different cross-platform differences are automatically set to the correct value.

      By default, the print statement adds a line break to the end of the output content. You can cancel this operation by adding a comma after the statement. The truncate () method accepts the optional size as the parameter. If given, the file is truncated to a maximum of size bytes. No, the pointer to the current file is truncated. Tell () returns the starting byte position.

      Standard file: standard input (generally a keyboard), standard output (buffer output to the display), standard error (non-buffered output to the screen), named the same as c, stdin, stdout, stderr. Access handle: import the sys module and use sys. stdin, sys. stdout, sys. stderr. print statements are usually output to sys. stdout. the built-in raw_input () is usually from sys. stdin accepts input.

      The sys. argv attribute provides access to command line parameters. The command line parameter is a parameter other than the program name when calling a program. Sys. argv [0] is always the name of the program

      The main interface for OS modular access control system functions. The OS module also handles most file system operations, including deleting/renaming files, traversing directory trees, and managing file access permissions. OS. path can perform operations on path names. It provides functions to manage and operate all parts of the path names of file files, obtain file or subdirectory information, and query file paths.

  3. Permanent storage module

    Data flattening (or data serialization and data sequencing) converts objects that are more complex than basic types into a binary data set, in this way, you can save the data set or send it over the network, and then restore the data set to the original object format.

    1. DBM-style modules:

      Multiple implementations: dbhash/bsddb, dbm, gdbm, dumbdbm. We recommend that you use the anydbm module, which automatically selects the most suitable one. They can only store strings and Cannot serialize Python objects.

    2. Shelve module:

      The shelve module uses anydbm to select the most suitable DBM module, and then uses cPickle to complete the storage conversion process.

    3. Pickle and cPickle

      Use the pickle module to save Python objects directly to files. You do not need to care about it. The pickle module has two functions: dump () and load (). Dump () accepts a file handle and a data object as the parameter, and saves the data object to the specified file in a specific format. The load () function extracts the saved object from the file. CPickle is a faster C language version of pickle.

Errors and exceptions
  1. Detect and handle exceptions

    Try-Statement t: Statement Syntax:

    try: try_suite # watch for exceptions here except Exception[,reason]: except_suite # exception-handling code 

    A try can certainly contain multiple types of exceptions, which are used to handle multiple types of exceptions respectively. Multiple exceptions must be handled by a distinct t, and the exceptions must be placed in a single tuples.

    When the function does not explicitly return a value, it returns None

    Catch all common exceptions: Failed t Exception: or use the bare precondition t clause failed t :( not recommended @ deprecated)

    Sys. exc_info () returns the information about the most recent exception captured by the T statement.

    Two special exceptions: SystemExit is because the current Python application needs to exit, KeyboardInterupt indicates that the user has pressed the CTRL-C (^ C) and wants to close Python. They are at the same level as Exception. To accept all exceptions including them, use the bare handle T or handle T BaseException

 

 

Binary and text files

Data as files is stored in binary format in computers. The distinction between text files and binary files is not physical, but logical.

A text file is a character sequence, which is based on character encoding and stores the character encoding (such as ASCII code) sequence. A binary file is a byte sequence based on value encoding, or there is no special encoding. It is in the same form as the data in the memory. It is only a byte sequence consisting of binary numbers.

Differences
  1. The concepts of "text files, binary files" and "opening in text mode and opening in binary mode" are completely different.
  2. Whether to open the file in binary mode, that is, whether to call fopen with B, does not affect the results written in binary mode using fwrite, whether in Linux or Windows; the results written in text format using fprintf are only affected in Windows. If B is not included, conversion from '\ n' to' \ r \ n' is performed, this conversion is not available when it comes to B, which does not affect writing in Linux.
  3. When a text tool opens a file, it first reads the binary bit stream corresponding to the file physically, then interprets the stream according to the decoding method specified by the Protocol, and then displays the interpretation result. For example, according to the ASCII code form, it will be 8 bit8bit interpretation file streams.
It seems that in Linux, do not care about it. In windows, if it is not opened in B mode, the line break will be treated as '\ n. The conversion between '\ r \ n' and' \ n' is completed in the background. To read data in the mode of B, consider '\ r \ n... This article is well written ..
Related Article

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.