Simple file operations

A brief file operations

File operations are invoked by the existence of the file on your hard disk to memory, people operate on file data in memory by the operating system.

Memory: unicode characters

HDD: utf-8 character

General data file character encoding is larger than the storage space with utf-8 character encoding to unicode storage, however, whatever a user enters characters into memory, unicode are compatible. Likewise, the hard drive no matter what encoding the file is read into memory, are compatible unicode.

Only unicode encoded in memory, and the rest are encoded in the hard disk.

(Memory) Unicode characters using binary encoding (encode) → (hard disk) utf-8 binary characters

(Hard disk) utf-8 binary character decoding (decode) → (memory) binary character Unicode

To ensure that files are not garbled core is to ensure that you get what what encoding with encoding memory, attention and text encoding interpreter code to be consistent.

supplement:

In Python2, the default character encoding ascill code, because it was the unicode code has not prevailed in Python3, the default character encoding is utf-8 encoding, the beginning of the file to #coding: utf-8, with English characters. It is to allow all the computers can be identified.

pycharm is the default character encoding: utf-8

window operating system's default character encoding is: GBK

Open the file in three modes

1, r mode,

  Read-only, if the file does not exist will complain

2, w pattern

  Write only if the file does not exist, create a file, write content

  If the file exists, the first file of the contents emptied and the contents written into

3, a pattern

  If the file exists, then later append content

  If the file does not exist, create a file, write content

Open the file syntax:

with open (r 'file name with the suffix', mode = 'open mode', encoding = 'encoding format file') as f:

r: the escape character is used to escape 'file name with the suffix' path

File name with the suffix: it can be an absolute path, or a relative path

File encoding format: file with the same coding mode can be opened

f: file objects built, to facilitate later using

Illustrate the operating mode using file

r mode:

Open with ( ' a.txt ' , MODE = ' R & lt ' , encoding = ' UTF-. 8 ' ) AS F:
       Print (f.readable ())      # determines whether or not the text can be read 
      Print (reached, f.read ())            # read all 
      Print (f.readline ())    # once, print a 
      Print (f.readlines ())   # once, print the contents of all rows, returns a list of

w Mode:

# W write-only mode: create empty document in the file does not exist, there is will empty the file, the file pointer will go to the beginning of the file 
with Open ( ' b.txt ' , the MODE = ' w ' , encoding = ' UTF-8 ' ) AS f: 
    f.write ( ' hello \ the n- ' ) 
    f.write ( ' I'm so \ the n- ' ) 
    f.write ( ' hello everyone \ the n- ' )
   # Note: 
# in the case file is not closed, continuous writing, after writing the content must follow behind the front to write the contents of 
# if you reopen the file to w mode, it will empty the contents of the file

a pattern:

# A write-only append mode: create empty document in the file does not exist, there is a file pointer file will move directly to the end of the file 
with Open ( 'c.txt', the MODE = ' A ' , encoding = ' UTF-8 ' ) AS F: 
    f.write ( ' 44444 \ n- ' ) 
    f.write ( ' 55555 \ n- ' )

 

Open the file in three modes

1, r mode,

  Read-only, if the file does not exist will complain

2, w pattern

  Write only if the file does not exist, create a file, write content

  If the file exists, the first file of the contents emptied and the contents written into

3, a pattern

  If the file exists, then later append content

  If the file does not exist, create a file, write content

Open the file syntax:

with open (r 'file name with the suffix', mode = 'open mode', encoding = 'encoding format file') as f:

r: the escape character is used to escape 'file name with the suffix' path

File name with the suffix: it can be an absolute path, or a relative path

File encoding format: file with the same coding mode can be opened

f: file objects built, to facilitate later using

Illustrate the operating mode using file

r mode:

Open with ( ' a.txt ' , MODE = ' R & lt ' , encoding = ' UTF-. 8 ' ) AS F:
       Print (f.readable ())      # determines whether or not the text can be read 
      Print (reached, f.read ())            # read all 
      Print (f.readline ())    # once, print a 
      Print (f.readlines ())   # once, print the contents of all rows, returns a list of

w Mode:

# W write-only mode: create empty document in the file does not exist, there is will empty the file, the file pointer will go to the beginning of the file 
with Open ( ' b.txt ' , the MODE = ' w ' , encoding = ' UTF-8 ' ) AS f: 
    f.write ( ' hello \ the n- ' ) 
    f.write ( ' I'm so \ the n- ' ) 
    f.write ( ' hello everyone \ the n- ' )
   # Note: 
# in the case file is not closed, continuous writing, after writing the content must follow behind the front to write the contents of 
# if you reopen the file to w mode, it will empty the contents of the file

a pattern:

# A write-only append mode: create empty document in the file does not exist, there is a file pointer file will move directly to the end of the file 
with Open ( 'c.txt', the MODE = ' A ' , encoding = ' UTF-8 ' ) AS F: 
    f.write ( ' 44444 \ n- ' ) 
    f.write ( ' 55555 \ n- ' )

 

Guess you like

Origin www.cnblogs.com/xy-han/p/11817160.html