Python full stack development: Python file processing

Source: Internet
Author: User

Python file processing

Mode of open File

File Common functions

One, open the file, close the file

Process of manipulating files:

# 1. Open the file, get the file handle and assign the value to a variable #2. Manipulating files by handle #3. Close File

Open () Method: Returns a File object

Syntax format:

# file = open (filename,mode,encoding) #打开文件 # file operation    file read and write  #file.close () #关闭文件

Common parameters

filename: The name of the file to open (the path to the file)

Mode: Open the file (see)

encoding: Encoding format for open files

Second, file read operation (text)

The premise that the file being read A.txt already exists

A.txt File Contents

This one readable file 11 This one readable file 22 This one readable file 33

F.read () reads all the characters of a file

# Open File (if a.txt exists)f = open ('a.txt', mode='r', encoding ='utf-8')print(F.read ())
F.close ()

F.readline () reads a line from a file

f = open ('a.txt', mode='r', encoding='utf-8  ')print(F.readline ())
F.close ()

F.readlines () reads all the rows in the file and returns a list as a behavior order element

f = open ('a.txt', mode='r', encoding='utf-8  ')print(F.readlines ())
F.close ()

Loop read

f = open ('a.txt', mode='r', encoding='utf-8  ') for in F:    print(i)    f.close () 

Pointer

f = open ('a.txt', mode='R', encoding='Utf-8')Print(F.readline (), end="')Print(F.readline (), end="')Print(F.readline (), end="')Print(F.readline (), end="')Print(F.readline (), end="') F.close ()

As we can see from the above results, we read a total of 5 rows, showing only three rows. Because there is a pointer in the file reading process, where the file is read, the pointer will point to where, after reading three lines, the pointer is already at the end of the file, there will be no content in the read operation of the file.

Conclusion: where to read, where the pointer is pointing, the pointer is at the end and the content is not read

Summary:

R: Default open mode, read-only mode
Note: When the file does not exist, an error

Third, file write operation (text) write-only mode (W)

F.write ()

F.writelines ()

F=open (R'A1.txt', mode='W', encoding='Utf-8')#the default is WTF.write ('first line \ n') F.write ('second line \ n') F.writelines (['1111\n','2222\n','3333\n'] # Writes a string list to a file f.write ('aaaaa\nbbbb\nccccc\n')

Summary:

W: Write-only mode
1. When the file is present, empty
2. Create an empty document when the file does not exist

Append Mode (a)

#A: Append write mode only#Note:#Create an empty file when the file does not exist#when the file exists, the cursor runs directly to the end of the fileF=open ('Access.log', mode='a', encoding='Utf-8')#print (f.writable ())#f.readlines () #报错F.write ('5555555555555\n') F.close ()

Summary:

A: Append write mode only
1, when the file exists, do not empty, add at the end
2, when the file does not exist, create an empty document, add at the end

Iv. file read operation (binary) Five, file write operation (binary)

Not yet ready to add

Python full stack development: Python 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.