Python path-File handling

Source: Internet
Author: User

I. Document processing process

Character encoding: Compiling a person's understanding of a character into a number that a machine can recognize

Two. Basic operation

1. Two modes of reading

(1) F=open (' Test.txt ', ' R ', Encoding=utf-8) #应用程序打开F, operating system open open file, default read mode, character encoding is UTF-8, read mode in file does not exist is unable to create file, error

Print (F.read ()

F.close ()

(2) F=open (' Test.txt ', ' RB ') #以byte模式读取文件

Print (F.read (' Hello ', Enceod (' UTF-8 '))

2. Various file operations

' R ' mode, read only, not present will error

' W ' mode, write only, does not exist will be created, presence will overwrite

' A ' mode, append, not exist will be created, exist then append at the end of the file, the cursor jumps to the last

F.read () #默认读取所有, f.readable, determines whether it is readable, f.readline () reads by line, F.readlines () reads all at once, and puts it in the list.

F.tell () viewing the cursor position

With open (' Test.txt ', ' W ', encoding= ' UTF-8 ') as F:

F.write () #打开文件赋值给f, performs a write.

Cursor movement of the file:

Read (3), when the file is opened as text, reads 3 characters, opens mode to B mode, reads 3 bytes

Seek three ways to move:

Seek (0), 0 mode moves to the beginning of the file by default, with the beginning as the reference bit by default

Seek (3,1), 1 mode is based on the position of the current cursor as a reference, 3 bytes after the move, only in B mode use

Seek ( -3,2), 2 mode defaults to the end of the file as a reference, the forward-cut cursor

1, move end of file

Python path-File handling

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.