Day 09 text manipulation

What is the file

File is a unit of virtual hard disk read and write the operating system for the user or application provides. File operation is based on operating core file, that file is: read and write. and also

As long as the file operation is that we want to initiate a request to the operating system, and then converts the user by the operating system or application file read and write operations to the hard disk collective command (such as a disk rotation control, controlling a mobile robot, in order to read data).

Why should file?

The memory could not permanently store data, whenever we want to permanently save the data needs to save the file to your hard drive, the file can be achieved while operating the hardware operation.

All in all open files divided into three steps:

Open the file
to read and write
closed

The absolute and relative paths

1. absolute path to the
Windows system absolute path from the drive letter (C:, D :) began to write a full path.
2. The relative path
relative to the currently executing file folder where to start looking.

The three file Open

Oppen (path, mode, encoding)
base mode operation, there are three files (default operating mode is the mode r):

  • r read-only mode to read mode, can only be read but not write, being given file does not exist
    because all content f.read () one time to read the file, if the file is very large, it may cause memory ringing off the hook, that is, computer card dead. So you can use f.readline () / f.readlines () to read the file contents.

  • w mode to write only write, can not read, there is time to write the contents of the file back to the file and then emptied; file does not exist when the content is written after the file is created.
  • append a mode
    may be added. File exists, write to the end of the file; the file does not exist when the content is written after the file is created.
    Read the contents of the file format, there are two (the default mode for content read mode b):

  • t text mode
  • b mode bytes b mode is the common mode because all files are on the hard disk to store binary form, should be noted that: b mode read and write files, must not add encoding parameters, because the binary coding can no longer .
    Note that: t, b Both modes can not be used alone, with the need to r / w / one of a conjunction.

document management with the operational context

Before we use the open () method of operating a file, open the file open but we also need to manually release the files take up the operating system. But in fact, we can be more convenient to open a file that provides context management tools Python Open --with ().
With open () method not only provides a method for the automatic release of the operating system takes up, with open and can be separated by a comma, a one-time open fast copy multiple files, file the

with open('32.txt', 'rb') as fr, \
     open('35r.txt', 'wb') as fw:
    f.write(f.read())

File pointer

1.seek (offset, whence): offset pointer represents the file offset in bytes
2.tell (): are counted every time from the beginning of the file to the current pointer location
3.read (n): Only in read (n), n mode is represented by the number of characters, in addition, relates to another whenever the number of bytes of the file pointer is
4.truncate (n): truncate (n ) is truncated file, the file the open must be written, but can not be opened with or w + w, etc., because the file directly as empty, so truncate () to be tested under the effect of r + a + or the like or a pattern. It's always a reference file header. And truncate () without parameters, corresponding to empty it.

Two ways to modify the contents of 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.
One way: one time to read all
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('37r.txt') 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: read line by line
the contents of the file stored in the hard disk into memory is read row by row, the new file is written modification is completed, and finally covering 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/bladecheng/p/10939529.html