python-day10--file Processing

Source: Internet
Author: User

1. File: Is the concept provided by the operating system

2. Open (r+ ' file path ', ' Open with ', ' what character encoding ') #r represent the original string

Eg:open (R ' C:\Users\13264\Desktop\aaaa.py ', ' R ', encoding= ' utf-8 ')

3. File Open:

F=open (R ' aaaa.py ')

This process is equal to two things: the first is the operating system to open the file, the second is to open space in memory a variable

4. File Recycling:

F.close # This is the memory occupied at the shutdown OS level

5. How to handle the file:

① text file: read-only mode, file does not exist error

Read readable ReadLine readlines

Extension: Move the cursor to seek

② text File: Write-only mode, file does not exist will be created, file exists will empty file contents

Write writeable Writelines

③ text file: Append write mode only, ' A ', file does not exist then create, exist then add content at the end of text

④b Mode: bytes mode

#rb
# f=open (' aaaa.py ', ' RB ')
# Print (F.read (). Decode (' Utf-8 '))

# f=open (' 1.jpg ', ' RB ')
# Data=f.read ()

#wb
# f=open (' 2.jpg ', ' WB ')
# f.write (data)


# f=open (' New_3.txt ', ' WB ')
# f.write (' aaaaa\n '. Encode (' Utf-8 '))

#ab
F=open (' new_3.txt ', ' AB ')
F.write (' aaaaa\n '. Encode (' Utf-8 '))

6. Context Management: Don't worry about file close

# with Open (R ' aaaa.py ', ' R ', encoding= ' Utf-8 ') as read_f,\
# Open (R ' aaaa_new.py ', ' W ', encoding= ' Utf-8 ') as Write_f:
# Data=read_f.read ()
# Write_f.write (data)

7. #循环取文件每一行内容

  With open (' A.txt ', ' R ', encoding= ' utf-8 ') as F:
# while True:
# Line=f.readline ()
# if not line:break
# Print (line,end= ")

# lines=f.readlines () #只适用于小文件
# Print (lines)
For line in F: #推荐使用
         Print (line,end= ")



8. Modification of documents

#方式一: For small files only
# import OS
# with open (' A.txt ', ' R ', encoding= ' Utf-8 ') as read_f,\
# Open (' A.txt.swap ', ' W ', encoding= ' Utf-8 ') as Write_f:
# Data=read_f.read ()
# Write_f.write (data.replace (' alex_sb ', ' ALEX_BSB '))
#
# os.remove (' A.txt ')
# os.rename (' A.txt.swap ', ' a.txt ')


#方式二:
Import OS
With open (' A.txt ', ' R ', encoding= ' Utf-8 ') as read_f,\
Open (' A.txt.swap ', ' W ', encoding= ' Utf-8 ') as Write_f:
For line in Read_f:
Write_f.write (Line.replace (' ALEX_BSB ', ' BB_ALEX_SB '))

Os.remove (' A.txt ')
Os.rename (' A.txt.swap ', ' a.txt ')

python-day10--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.