Basic operation of file

1. The concept of files

  • The computer’s files are stored in some kind of A section of data
  • on long-term storage devices

  • Long-term storage devices include: hard drives, USB flash drives, mobile hard drives, optical discs…

The role of the file: Save the data for a long time, and use it when needed.
File storage method: In the computer, the file is in the form of
Text file saved on disk in a binary
way: You can use text editing software to view it, which is essentially a binary file
Binary file: The saved content is not for people to read directly, but is provided for use by other software, such as image files, audio files, video files, etc., binary files cannot be used Text editing software View

Two, basic file operations

< strong>In Python you need to remember 1 function and 3 methods to manipulate files

open The function is responsible for opening File and return the file object
read/write/close All three methods need to be called through the file object
< /p>

share picture

Example of reading file

# 1\. Open-File name needs to be capitalized

file = open("README")

# 2\. Read
text = file.read()
print(text)

# 3\. Close
file.close()

3. Ways to open files

Share pictures

Example of writing file

# Open file

f = open("README", "w")

f.write(
"hello python!\n ")
f.write(
"It’s really nice today< span style="color: #800000;">")

# Close file
f.close()

# Open file

file = open("README")

while True:
# read a line of content
text = file.readline()

# Determine whether to read the content
if not text:
break

# There is already a `\n` at the end of each line read
print(text, end="")

# Close file
file.close()

Four. File management operations

Share pictures

V. Encoding format of text files

  • The content stored in text files is a file based on character encoding, which is common The encoding includes ASCII encoding, UNICODE encoding, etc.
  • Python 2.x uses ASCII encoding format by default, Python 3.x uses UTF-8 encoding format by default
  • ASCII Encoding

    • Only 256 ASCII characters in the computer
    • One ASCII< /code> Occupies 1 byte space in the memory
      • 8 0/1 permutation and combination methods total There are 256 types, that is, 2 ** 8

    UTF -8 encoding format

    • The computer uses 1~6 bytes to represent a UTF-8 Characters, covering texts in almost all regions on the earth
    • Most Chinese characters use 3 bytes to represent
    • UTF-8 is an encoding format of UNICODE encoding

    How to use Chinese in Ptyhon 2.x?

    • Add code in the first line of the Python 2.x file# *-* coding:utf8 *-* , The interpreter will process python files with utf-8 encoding, this method is officially recommended!
    • You can also use # coding=utf8.

    How to traverse unicode strings correctly in Python 2.x?

    • In Python 2.x, even if the file is specified to use the UTF-8 encoding format, it will still < strong>Traverse the string in bytes
    • To be able to traverse the string correctly, When defining the string, Enclose the string in quotation marks Before, add a lowercase letter u to tell the interpreter that this is a unicode string (using UTF-8 encoding format) String)

    # *-* coding:utf8 *-*
    

    # Before the string, add a `u` to indicate that the string is a utf8 String
    hello_str = u"Hello world"

    print(hello_str)

    for c in hello_str:
    print(c)

1. The concept of files

  • The computer’s files are stored in some kind of long-term storage device strong> a paragraph of data
  • Long-term storage devices include: hard disk, U disk, mobile hard disk, CD-ROM...

The role of the file: Save the data for a long time, and use it when needed.
File storage method: On the computer, the file is in binary
Text file saved on disk in the way of
: You can use text editing software to view, it is essentially a binary file
Binary file >: The saved content is not for people to read directly, but is provided for use by other software, such as: image files, audio files, video files, etc., binary files cannot be used for text editing Software View

# 1 \. Open-File name needs to be capitalized

file = open("README")

# 2\. Read
text = file.read()
print(text)

# 3\. Close
file.close()

# Open file

f = open("README", "w")

f.write(
"hello python!\n ")
f.write(
"It’s really nice today< span style="color: #800000;">")

# Close file
f.close()

# Open file

file = open("README")

while True:
# read a line of content
text = file.readline()

# Determine whether to read the content
if not text:
break

# There is already a `\n` at the end of each line read
print(text, end="")

# Close file
file.close()

ASCII encoding

  • Computer Only 256 ASCII characters
  • One ASCII occupies 1 byte in memory > Space
    • 8 There are a total of 256 kinds of permutations and combinations of 0/1, that is, 2 ** 8

UTF-8 encoding format

< ul>

  • The computer uses 1~6 bytes to represent a UTF-8 character, covering almost all regions on the earth strong>
  • Most Chinese characters will use 3 bytes to represent
  • UTF-8 is UNICODE An encoding format for encoding
  • How to use Chinese in Ptyhon 2.x?

    • Add code in the first line of the Python 2.x file# *-* coding:utf8 *-* , The interpreter will process python files with utf-8 encoding, this method is officially recommended!
    • You can also use # coding=utf8.

    How to traverse unicode strings correctly in Python 2.x?

    • In Python 2.x, even if the file is specified to use the UTF-8 encoding format, it will still < strong>Traverse the string in bytes
    • To be able to traverse the string correctly, When defining the string, Enclose the string in quotation marks Before, add a lowercase letter u to tell the interpreter that this is a unicode string (using UTF-8 encoding format) String)

    # *-* coding:utf8 *-*
    

    # Before the string, add a `u` to indicate that the string is a utf8 String
    hello_str = u"Hello world"

    print(hello_str)

    for c in hello_str:
    print(c)

    Leave a Comment

    Your email address will not be published.