The basic data structure of the file operation Python

Operation of a file with the word process is as follows:

1, locate the file, double-click to open

2, read or modified

3. Save & Close

Operation with python file that is about:

1 f = Open (filename)   # Open File 
2 f.write ( " I'm a wild programmer " ) # write 
3 f.read ()   # Read 
4 f.close () # Save and Close

But one thing human flesh with different operating word document, word document is opened as long as it can be read that is, they can be modified. Python but more abnormal, can only be read, create, append any of the three modes in an open document, that is, can not read and write.

1, read-only mode ( read mode read only )

 

. 1 F = Open ( ' small mountains ' , ' R & lt ' , encoding = ' UTF8 ' ) # read mode can only write mode, write-only 
2 Data = (. 8) reached, f.read # which value was taken out of the number of characters in Britain python3 letters and characters are a character learning Hello 
. 3  Print (Data)
 . 4 f.close ()

 

2, write-only mode ( write mode can only be written at the same time create the object in the original text contents emptied out )

 

1 f = Open ( ' small mountains 2 ' , ' w ' , encoding = ' utf8 ' )   # write mode can only be written at the same time creating an object of the text in the original contents emptied out 
2                                            # If no file name to create a new file 
. 3  Print (() f.fileno)   # on the operating handle is essentially an object 
. 4 f.write ( ' the Hello World \ n- ' )
 . 5 f.write ( ' Xiaohu ' )
 . 6 f.close ()   # close the file

 

3, append mode ( add new content to the file in append mode content )

 

. 1 F = Open ( ' small mountains 2 ' , ' A ' , encoding = ' UTF8 ' )   # 'A' add new content in the contents of the file append mode 
2 f.write ( ' \ nHello World \ n- ' )
 . 3 F .write ( ' Alex ' )
 . 4 the time.sleep (. 5 )
 . 5  # f.close () closes the file #

 

4, mixed mode

(. 1) R & lt + (read-write)

. 1 F = Open ( ' small mountains ' , ' R & lt + ' , encoding = ' UTF8 ' )
 2  Print (f.readline ())
 . 3 f.write ( ' Fei ' )
 . 4  for I in F:
 . 5      Print (i.strip ()) # line by line reading 
6 f.close ()

 

(2) w + ( write-read mode, W + first empty )

 

. 1 F = Open ( ' small mountains ' , ' w + ' , encoding = ' UTF8 ' )
 2  Print (f.readline ()) # could not be read because of w + to empty the contents 
. 3 f.write ( ' Fei ' )
 . 4  Print ( f.tell ()) # or can not reproduce the content, since the cursor after the input of the content, not the content is behind 
. 5 f.seek (0) # to display in the console, it is necessary to move the cursor 
. 6  for I in F:
 . 7      Print (i.strip ()) # after the movement of the cursor can see the contents 
. 8 f.close ()

 

(3) a + (additional read mode)

 

1 f = open('小重山', 'a+', encoding='utf8')
2 print(f.tell()) # 在文本内容末尾
3 print(f.readline()) # 此时控制台是没有显示任何内容的,因为光标在'a+'模式中在内容的末尾
4 f.close()

 

 

用法

1、文件操作之flush及遍历文件最优算法(1)

 

 1 print(f.readline())  # 输出文件第一行的内容 换行符也打印了
 2 print(f.readline())  # 输出文件下一行的内容
 3 print(f.readlines())   # 使用列表将一行一行的内容输出 换行符\n也输出
 4 number = 0
 5 a = f.readlines()  # 使用列表将一行一行的内容存起来
 6 for i in a:  # 使用for循环来将列表内容输出
 7     number += 1
 8     if number == 2:
 9         # print(i.strip(), 'xiaohu')  # 加strip()方法去掉换行
10         i = '*****'.join([i.strip(), 'iiii']) # 字符串拼接用 join 不要用 +
11     print(i.strip())
12 f.close()  # 关闭文件

 

2、文件操作之flush及遍历文件最优算法(2)

 1 f = open('小重山2', 'a', encoding='utf8')
 2 
 3 for i in range(30):
 4     sys.stdout.write("*") # stdout 是一个终端的输出显示
 5     sys.stdout.flush()
 6     time.sleep(0.2)  # 每隔0.2秒显示
 7 
 8 for i in range(30):
 9     print('*',end=' ',flush=True)
10     time.sleep(0.2)  # 每隔0.2秒显示
11 
12 f.truncate(5) # 在光标30处后面全删 'w'中全部格式化 ‘a’中正常截断
13 f.write('Hello world')
14 f.truncate(5)
15 f.close()

3、在某一行末尾添加字符串添加

 

1 f = open('小重山', 'r+', encoding='utf8')
2 number=0
3 for line in f:
4     number+=1
5     if number==6:
6         line=''.join([line.strip(),'xiaohu'])
7         # f.write('xiaohu') # 写永远是在最后一行写 所以直接write()行不通!!!!!!! 因为存储机制决定了无法修改
8     print(line.strip())
9 f.close()

 

4、将一个文件里的内容写到另一个文件里,并且修改某一行

 

 1 f_read=open('小重山','r',encoding='utf-8')
 2 f_write = open('小重山2','w',encoding='utf-8')
 3 number = 0
 4 for line in f_read:
 5     number+=1
 6     if number==5:
 7         print(line)
 8         line='hello 岳飞\n' # 将这一行内容进行修改
 9         print(line)
10     f_write.write(line) # 将一个文件里的内容一行一行写到另一个文件里
11 f_read.close()
12 f_write.close()

 

5、文件操作之with方法

 

with open('log','r') as f: # 只要退出with代码块,自动帮你关,相当于f.close(),
    f.readline()
    f.read()

with open('log1','r',encoding='utf-8') as f_read,open('log2','w',encoding='utf-8') as f_write: # 可以同时管理多个文件
                                                             # 相当于f_read=open('小重山','r',encoding='utf-8')
                                                             # f_write = open('小重山2','w',encoding='utf-8')
    for line in f_read:
        ....

 

  

Guess you like

Origin www.cnblogs.com/wyh-study/p/11324097.html