python - File I/O


  • r : read only
  • rb : read only in binary format
  • r+ : read and write, doesn't delete the content of the file, doesn't create a new file if  file doesn't exist, if read before write,it will write for appending
  • rb+ : r+ in binary format
  • r : write only
  • wb : write only in binary format
  • w+ : read and write,deletes the content of the file , creates it if it doesn't exist
  • wb+ : w+ in binary format
  • a : open file for appending, creates it if it doesn't exist
  • ab : a in binary format
  • a+ : read and write for appending
  • ab+ : read and write for appending in binary format

file = open("test.txt", "w")
file.write('542156')
file.close()
file = open("test.txt", "r")
data = file.read()
print (data)#542156
file.close()

留言