day09 finishing

day09 finishing

What is document processing

Modify the stored information

What is file: the concept of a virtual operating system, the storage information

Process file operations

1. Open the file

f = open (r 'path', 'mode', the character encoding)

2. Modify / read files

f.write (content)

data = f.read()

for i in f: reading

3. Save the file

f.flush

4. Close the file

By an operating system file

f.close

Read the file / Write / Append

Rt read_text read read read-only text

data = f.read

data = f.readline()

data = f.readlines()

Read a line of text less line, there is a default at the end of each line wrap

for i in fr :

wt: Write only (w re-written from the beginning of the file), unreadable

f.write (content)

at: additionally write only

f.write ( 'content')

encoding specifies the encoding format write

Read encoding format encoding specified file

Open the file in two ways

t ----> Text

b ----> binary

Path to the file

File location on the hard disk

The concept of absolute and relative paths

Writing project with a relative path

with management context

with a file automatically closes

The file is not indented under the close operation, the file operation in this indent

You can not read into memory at this indent

Advanced application file

r + readable and writable

Write the words cursor in the header, covering the following character

w + readable writable

W no difference between writing and reading, then put behind the content Qingdiao

a +

Readable but not read, then read something cursor at the end of file

Cursor movement

seek to move the cursor

tell find the location of the cursor

truncate truncate behind the content

Open multiple files

with open ( 'path', 'mode', encoded) as fr, open ( 'path', 'mode', encoded) as fw:

​ for i in fr :

fw.wirte (i)

​ fw.flush()

import os

The os.remove ( 'name')

os.rename ( 'old', 'new')

Guess you like

Origin www.cnblogs.com/kaizi111/p/11567722.html