Python Road--day6---file processing

Source: Internet
Author: User

I. Documentation
1. File is the operating system provided to the application to operate the virtual concept of hard disk, the user or application through the operation of files,
You can permanently save your data.

2. Operation Process
#1. Open the file, get the file handle and assign a value to a variable--f = open (' Xx.txt ', mode= ', encoding= ')
#2. Manipulating a file through a handle read-only f.read R mode-write-write F.write w mode---Append only a mode
#3. Close File F.close

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 a file with a handle
Data=f.read ()

#3. Close File
F.close ()


II: 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

Emphasize:
1. File Close and resource recovery
Opening a file consumes two parts of the resource:
1. The operating system opens the file
2, Application variables
When you have completed a file, you must recycle the two parts of the file with a non-landed method:
1, F.close () #回收操作系统级打开的文件
2, Del f #回收应用程序级的变量 (must occur after f.close, Python garbage collection mechanism, we
You must consider the recycling of the application's variables)

1.1. Omit F.close, use the WITH keyword to manage the context, and implement automatic F.close
With open (' A.txt ', ' W ') as F:
Pass

With open (' A.txt ', ' R ') as Read_f,open (' B.txt ', ' W ') as Write_f:
Data=read_f.read ()
Write_f.write (data)


2.f = open () The character encoding type of the opening file
No case specified: Windows platform--GBK
Linux Platform--utf-8



Three. Open the file mode
The mode of opening the file has (default is text mode):
R, read-only mode "default mode, file must exist, not present, throw exception"
F.read () #读取所有内容, the cursor moves to the end of the file
F.readline () #读取一行内容, the cursor moves to the beginning of the second
F.readlines () #读取每一行内容, stored in the list

W, write-only mode "unreadable; not exist" created; empty content "
F.write (' 1111\n222\n ') #针对文本模式的写, you need to write your own line break
F.writelines ([' 333\n ', ' 444\n ']) #文件模式

A, the Append write mode is "unreadable; it does not exist, it is created; only append content exists."
F.write (' 1111\n222\n ') #针对文本模式的写, you need to write your own line break
F.writelines ([' 333\n ', ' 444\n ']) #文件模式

Python Road--day6---file processing

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.