File operations in Python

Simple operation of files

  1. File opening
    wenjian = open('wenjian', encoding='utf-8')
  2. File reading
    wenjian = open('wenjian',encoding='utf-8')#File relative path, file encoding
    data = wenjian.read()#Read the content of the file
    print(data)
    print(wenjian.readable( ))#Whether the file is readable
    print(wenjian.readline())
    wenjian.close()
  3. File writing
    wen = open('wenjian','w',encoding='utf8')
    data= wen.write('Can you write in')
    print(data)
    wen.close()#This is after the file operation , to free memory
  4. Can read and write
    wenjian = open('wenjian','r+',encoding='utf8')
    data1 = wenjian.write('afsdg') wenjian.close
    ()
    wenjian = open('wenjian','r',encoding ='utf8')
    data = wenjian.read()
    print(data)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325252303&siteId=291194637