File processing (b)

File processing (b)

File Handling

What is file: the concept of virtual storage operating system provides a letter (to store information in binary)

What is file handling: modify the stored information

Operating procedures:

  1. open a file

    print('D:\上海python12期视频\python12期视频\day 09\test.py')
    print('D:\上海python12期视频\python12期视频\day 09\\test.py') # \\t--》第一个\会让第二个\失去意义
    print(r'D:\上海python12期视频\python12期视频\day 09\test.py') # r会让内部所有的\失效
  2. Modify / read files

    f.write('''
    孩儿立志出湘关,学不成名誓不还
    埋骨何须桑之地,人生无处不青山
    ''')
  3. save document

    f.flush() # 快速保存,你可以不使用
  4. Close the file

    f.close # 告诉操作系统关闭文件

The three file Open

  1. Read

    # 读取:rt   read_text 读文本内容,只读,不可修改
    f =open(r'D:\上海python12期视频\python12期视频\day 09\test.py', 'rt', encoding='utf8')
    
  2. Write

     # wt:只写(w清空文件后写入),不可读
    f = open(r'D:\上海python12期视频\python12期视频\day 09\test.py', 'wt', encoding='utf8')
    
  3. Additional writing

    # at:只写入(追加写入)
    f = open(r'D:\上海python12期视频\python12期视频\day 09\test.py', 'at', encoding='utf8')
    f.write('中')

    Open the file in two ways:

    t ---> text

    b ----> binary mode: The two things that are not used alone, with r / w / a in combination with, no encoding

    b mode is generally used for picture / audio / video save

Absolute and relative paths

Absolute path: from the beginning of the letter D: \ Shanghai python12 of video \ python12 of video \ day 09 \ test.py

Relative path: file name of the executable file (currently running file) files, executable files and open files that belong to the same file

Try to use a relative path

f = open('test.py','r',encoding='utf8')
print(f.read())

with file management context

with: providing an automatic shut down file (the lifting of the occupation of the operating system)

with pen('text.py','r',encoding='utf8') as f:
    date = f.read   # data放到python的内存中
    
print(date)
# 关闭文件(操作系统),没有关闭python内存中的文件

Advanced application file

Three new models:

  1. Readable and writable: r +

    with open('text.py','r+',encoding='utf8')
     fr.write('高级')  # 光标在文件头部,覆盖后面的字符
  2. Readable and writable: w + w and not make any difference

    with open('text.py','w+',encoding='utf8')
     fr.write('高级')  # 先清空后写入
  3. Readable and writable: a + a default mode at the end of the cursor

    with open('text.py','a+',encoding='utf8')
     print(fr.read())    # 光标在文件末尾,所以显示为空
  4. File modification

    # 同时打开多个文件
    # (1)
    with open('test.py', 'r', encoding='utf8') as fr, \
             open('test_swap.py', 'w', encoding='utf8') as fw:
    
        date = fr.read()
        date= date.replace('sb','bs')
    
        fw.write(date)
    
    
    # (2)
    with open('test.py', 'r', encoding='utf8') as fr, \
             open('test_swap.py', 'w', encoding='utf8') as fw:
         # 再大的文件都能修改
         for i in fr:
             s = '傻逼'
             i = i.replace('sb', s)
             fw.write(i)
             fw.flush()  # 先保存成功再继续运行

Guess you like

Origin www.cnblogs.com/hj59988326/p/11544528.html