8.08 advanced application file

8.08 advanced application file

A readable, writable

  • r + t: readable, writable
  • w + t: write, read
  • a + t: appendable readable
# wt
with open('path', 'wt', encoding='utf-8') as fw:
    print(fw.readable())  # False
    print(fw.writable())  # True

with open('path', 'w+t', encoding='utf-8') as fw:
    print(fw.readable())  # True
    print(fw.writable())  # True

# r+t
with open('path', 'r+t', encoding='utf-8') as fr:
    print(fr.readable())  # True
    print(fr.writable())  # True

Second, the file pointer

Suppose we need to add the contents of a file in the middle row of content, if using a basic r / w / a pattern is very difficult to achieve, so we need to move the pointer within the file.

The hard disk has never modify a say on the hard disk only cover that new content covering the new content.

seek(offset,whence)

pointer offset represents the file offset in bytes

# seek()
with open('path', 'rt', encoding='utf-8') as fr:
    print(f"fr.seek(4, 0): {fr.seek(3, 0)}")  # 0相当于文件头开始;1相当于当前文件所在位置;2相当于文件末尾
    fr.seek(0,2)  # 切换到文件末尾

tell()

Every time statistics are from the beginning of the file to the current position pointer is located

# tell()
with open('path', 'rt', encoding='utf-8') as fr:
    fr.seek(4, 0)
    print(f"fr.tell(): {fr.tell()}")

read(n)

Only read (n) in the mode, n is the number of characters represents, in addition, provided that the other is directed to the number of bytes of the file pointer

# read()
with open('path', 'rt', encoding='utf-8') as fr:
    print(f"fr.read(3): {fr.read(3)}")

truncate(n)

truncate (n) is truncated file, Open file must be written, but can not be opened with w or w +, etc., as directly as empty files, so truncate () To test the effect of the r + or a or a + isochronous mode . It's always a reference file header. And truncate () without parameters, corresponding to empty it.

# truncate()
with open('path', 'r+t', encoding='utf-8') as fr:
    fr.truncate(3)

Extended file operations - modify the file

Data files are stored on the hard disk, so there is only covering, there is no modification is to say, we usually see modify the file, all simulated results, specifically implemented in two ways.

method one

The contents of the file stored in the hard disk all loaded into memory, can be modified in memory after the modification is completed, and then covered by the memory to the hard disk (word, vim, nodpad ++ editor, etc.).

import os

with open('path') as fr, open('37r_swap.txt', 'w') as fw:
    data = fr.read()  # 全部读入内存,如果文件很大,会很卡
    data = data.replace('tank', 'tankSB')  # 在内存中完成修改
    fw.write(data)  # 新文件一次性写入原文件内容
# 删除原文件
os.remove('37r.txt')
# 重命名新文件名为原文件名
os.rename('37r_swap.txt', '37r.txt')
print('done...')

Second way

Contents of the file stored in the hard disk into memory line by line, the modification is completed on the new file is written, and finally cover the source file with a new file.

import os

with open('37r.txt') as fr,open('37r_swap.txt', 'w') as fw:
    # 循环读取文件内容,逐行修改
    for line in fr:
        line = line.replace('jason', 'jasonSB')
        # 新文件写入原文件修改后内容
        fw.write(line)
os.remove('37r.txt')
os.rename('37r_swap.txt', '37r.txt')
print('done...')

All in all, modify the contents of the file ideas for: to open the original file read, write way to open a new file, the contents of the original file to be modified, and then written to the new file, and then use the os module methods, the original file delete, rename the new file to the original file name, to achieve the purpose of real ones.

Guess you like

Origin www.cnblogs.com/dadazunzhe/p/11322010.html