Python full stack development-day7 file processing

Source: Internet
Author: User
Tags throw exception

python full stack development-day7 file processingOne, the file operation

First, Introduction

Computer systems are divided into:computer hardware, operating systems, applicationsThree parts. Applications that we write in Python or other languages need to be saved on the hard drive if we want to keep the data permanently, which involves the application operating hardware, and it is well known that the application is not able to manipulate the hardware directly. The operating system encapsulates complex hardware operations into a simple interface for use by the user/application, where the file is the operating system provided to the application to manipulate the virtual concept of the hard disk, and the user or application can save its own data permanently by manipulating the file. With the concept of a file, we no longer have to consider the details of the operation of the hard disk, just the process of manipulating the files:#1. Open the file, get the file handle and assign a value to a variable#2. Manipulating files with a handle#3. Closing Files

Second, in Python

   #1. Open the file, get the file handle and assign a value to a variable f=open (' A.txt ', ' R ', encoding= ' utf-8 ') #默认打开模式就为r#2. Manipulating files with a handle data=f.read ()#3. Close File F.close ()

Iii. Process Analysis of F=open (' A.txt ', ' R ')

   #1, the application initiates a system call to the operating system open (...)#2, the operating system opens the file, and returns a file handle to the application#3, the application assigns a file handle to the variable F

Four, emphasize!!!  

# 1th: Open a file containing two parts of the resource: the operating system-level Open file + application variable. When you have completed a file, you must recycle the two parts of the file with a non-landed method: 1, F.close () # Recycle OS-level Open files 2,del F # Recycle application-level variables where del f must occur after f.close (), otherwise it will cause the operating system to open the file has not been closed, in vain to occupy resources, And the Python automatic garbage collection mechanism determines that we do not have to consider Del F, which requires us, after the completion of the file, we must remember F.close () although I say so, but many students will not forget the face of the F.close (), for these not long brain students, We recommend a fool-style operation: use the WITH keyword to help us manage the context
1With open ('a.txt','W') as F:2     Pass 3With open ('a.txt','R') as Read_f,open ('B.txt','W') as Write_f:4Data=Read_f.read ()5Write_f.write (data)
Second, open the file mode
File handle = open (' File path ', ' pattern ', ' character encoding ')

Patterns can be in the following ways as well as combinations between them:

Character Meaning
' R ' Open for reading (default)
' W ' Open for writing, truncating the file first
A Open for writing, appending to the end of the file if it exists
' B ' Binary mode
' t ' Text mode (default)
+ Open a disk file for updating (reading and writing)
U Universal newline mode (for backwards compatibility; Should is used in new code)
#1. The mode of opening the file has (default is text mode):R, read-only mode "default mode, file must exist, not present, throw exception"
1 with open ("duoduo.txt", mode='r', Encoding=utf-8 " ) as F:2     print(F.read ())
W, write-only mode "unreadable; not exist" created; empty content "
1 with open ("duoduo.txt", mode='w', Encoding=utf-8 " ) as F:2     print(F.write ("duoduo666"))
A, the Append write mode is "unreadable; it does not exist, it is created; only append content exists."
1 with open ("duoduo.txt", mode='a', Encoding=utf-8 " ) as F:2     print(F.write ("77777777"))
third, the method of operating the file

  

To be continued .....

Python full stack development-day7 file processing

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.