Python file operations Python file operation

Source: Internet
Author: User
Tags readable

The computer is composed of software, system and hardware.

Applications written in Python or other languages if you want to keep the data permanently, you need to write the data to the hard disk, which involves the application operating hardware, it is well known that the program can not directly manipulate the hardware, which is the use of the operating system. The system encapsulates the complex hardware operation into a simple interface/application, where the file is the virtual concept of the operating system to operate the hard disk, the user or the application operation file, can save the data permanently

Steps for file operation:

1. Open the file and return a statement handle and assign a value to a variable

2. Manipulating files through the returned statement handle

3. Close the file

# Open the file and get the statement handle assigned to a variable

f = open ("Test.txt", ' w ', encoding = "Utf-8")

#对文件操作

F.write ("11111")

#关闭文件

F.close ()

#1, application system to the operating system to launch an open (.... )

#2, the operating system opens the file, and returns a statement handle

#3, the application assigns a statement handle to a variable

#打开一个文件包含两个资源: One is the operating system level Open file, the other is an application variable, after the operation of a file needs to be the two parts of the file with the full collection of resources,

First method: F.close ()

The second method: Del F

F.close (), be sure to before Del F, otherwise the system open file is not closed, in vain to occupy resources. Python has an automatic garbage collection mechanism, no need to consider del F

The best way to use with open (' Test.txt ', ' W ') as F:

Pass

With open (' Test.txt ', ' R ') as Read_f, open (' Tate.txt ', ' R ') as Write_f:

data = Read_f.read ()

Write_f.write (data)

What encoding the file is written in and needs to be opened with the corresponding encoding

File open mode (default text mode):

R: Default read mode "default mode, file must exist, otherwise error"

W: Write-only mode "does not exist then creates, exists then empties content"

A: Append only write mode "unreadable, non-existent person created, existing only append content"

The above only applies to text mode, if you want to open the picture, video, you need to use B mode open, indicating the byte operation (picture, video, is stored in byte encoding, regardless of encoding)

Wb

Rb

Ab

Note: Open the file in B mode, read the content in bytes, write also need byte way, cannot specify encoding

# "+" means you can read and write a file at the same time

r+: Read-write "readable, writable"

w+: Write read "writable, readable"

A +: Write "readable, writable"

X: Write only "unreadable, do not create, otherwise error"

x+: Write read "Readable, writable"

Read (3), text mode: Read three strings, b mode: 3 bytes,

Cursor:

Seek (0), tell,truncate () are all one-byte units:

Seek there are three ways to 0,1,2, which must be done in B mode, truncate truncate the file, cannot be w/w+, that will empty the file, please in r+/a/a+ mode.

With convenience: To prevent open files and forget to close

There are two ways to modify a file: One at a time, after the operation, and then in writing to another file. Then remove the previous file and rename the file that was written to the deleted name

The second type: can be read in one line and then modified, and then saved to another file

File operations: Process

Find--How to open---off

f = open (' Test ', "w")

1, the way to read the file (the content obtained from the file is a string):

The Read method, a one-time reading of all the contents of the file

Second, F.read (5) A single part, read five characters, line breaks also count

Three, ReadLine, follow the line read, each execution readline will read the next line

Iv. readlines, returns a list that returns a list of each row in the list as an element of the list

Strip () Remove space, tab, line break

V. For loop: For I in F:

Print (i)

Python file operations Python file operation

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.