python ------- file operations

First, the document processing workflow

  1. Open the file, get the file handle and assigned to a variable

  2. Follow through on file handles

  3. Close the file

r mode, the default mode, the file does not exist error

w mode, the file does not exist, create the file exists cover

a mode, the file does not exist, create the file does not exist coverage, writing content will be added way to write (to write the log file when used), additional write mode is a special mode

b (rb, wb, ab) mode: without adding encoding: utf-8

  1 f=open('c.txt','rb')
  2 # print(f.read())
  3 print(f.read().decode())
  4 
  5 f=open('d.txt','wb')
  6 f.write('啦啦啦'.encode('utf-8'))
  7 f.close()

Second, the basic operation

1. Open the file mode

  File handle = open ( 'file path', 'mode')

When you open a file, you need to specify in what way the file path and open the file.

Open the file modes are:

  • r, [read-only mode the default mode, the file must exist, there is no exception is thrown]
  • w, write-only mode [unreadable; does not exist, create; there is then emptied the contents]
  • x, write-only mode [unreadable; it does not exist is created, then there is an error]
  • a, [append mode-readable; does not exist it is created; exists only additional content]
  1 # #只读模式
  2 # f=open(r'c.txt',encoding='utf-8')
  3 # # print('====>1',f.read())
  4 # # print('====>2',f.read())
  5 # # print(f.readable())
  6 # # print(f.readline(),end='')
  7 # # print(f.readline())
  8 # # print("="*20)
  9 # # print(f.the Read ())
 10 # Print (. f readlines ())
 11 # f. use Close ()
 12  
13 # to write mode: the file does not exist, create, overwrite the original file exists
 14 # f = Open ( " new.py ", 'W', encoding = 'UTF-. 8')
 15 # F. Write ( '1111111111 \ n-')
 16 # F. the writelines ([ '2222 \ n-', '2,222,548 \ n-', '978 646 \ n-' ])
 17 # f. use Close ()
 18  
19  
20 # append mode: file does not exist, create, does not overwrite the file exists, write the additional content is a way to write
 21 # f = Open ( ' new new.py','a',encoding='utf-8')
 22 # f.write('nishishui\n')
 23 # f.writelines(['aa\n','bb\n'])
 24 # f.close()
Basic Operation File View Code

"+" Indicates a file can read and write at the same time

  • r +, [reader readable, writable]
  • w +, W R {read, write}
  • x +, [read write read, write}
  • a +, [read write read, write}

"B" represents the manner in bytes

  • rb or r + b
  • w + b or wb
  • xb 或 w + b
  • ab some a + b

NOTE: When b in open manner, the read byte is the type of content, but also a need to provide byte type writing, you can not specify the encoding

Exercise, use b mode, writing a cp tool requirements are as follows:

  1. copy both text and can copy video, pictures and other files

  1 # b模式
  2 f=open('1.jpg','rb')
  3 data=f.read()
  4 # print(data)
  5 f=open('2.jpg','wb')
  6 f.write(data)
  7 print(data)

三、上下文管理

1. with open('a.txt','w')  as  f:

  pass

2.with open('a.txt','r')  as read_f,open('b.txt','w')  as  write_f:

  data=read_f.read()

  write_f.write(data)

四、文件的修改

  1 import os
  2 with open('a.txt','r',encoding='utf-8') as read_f,\
  3         open('a.txt.swap','w',encoding='utf-8') as write_f:
  4     for line in read_f:
  5         write_f.write(line)
  6 
  7 os.remove('a.txt')
  8 os.rename('.a.txt.swap','a.txt')

五、文件内光标移动

一: read(3):

  1. 文件打开方式为文本模式时,代表读取3个字符

  2. 文件打开方式为b模式时,代表读取3个字节

二: 其余的文件内光标移动都是以字节为单位如seek,tell,truncate

注意:

  1. seek有三种移动方式0,1,2,其中1和2必须在b模式下进行,但无论哪种模式,都是以bytes为单位移动的

    seek控制光标的移动,是以文件开头作为参照的。

   tell当前光标的位置

  2. truncate是截断文件,截断必须是写模式,但是不能用w或w+等方式打开,因为那样直接清空文件了,所以truncate要在r+或a或a+等模式下测试效果

  1 import time
  2 with open('test.txt','rb') as f:
  3     f.seek(0,2)
  4     while True:
  5         line=f.readline()
  6         if line:
  7             print(line.decode('utf-8'))
  8         else:
  9             time.sleep(0.2)

 

 

分类: python相关

Guess you like

Origin www.cnblogs.com/lz1996/p/11573039.html