1 minute read

Tags: ,

File Processing & Module

f = open(file="file.txt")
print(f.read())
  • cursor
f = open(file="file.txt")
print(f.read()) # print the content
print(f.read()) # print empth content
  • close file
f = open(file="file.txt")
f.close()
f.read()
  • with
with open("file.txt") as t:
    content = t.read()
print(content)
  • wiht text to a file
with open('file.txt', 'w') as file:
    file.write('test')

Append a file

Character Meaning
‘r’ open for reading (default)
‘w’ open for writing, truncating the file first
‘x’ create a new file and open it for writing
‘a’ open for writing, appending to the end of the file if it exists
‘b’ binary mode
‘t’ text mode (default)
’+’ open a disk file for updating (reading and writing)
‘U’ universal newline mode (deprecated)
  • Append a file
    • x: Only works when the file doesn’t exist
    • a: Append contents to the end of the file, and move the cursor to the end
    • +: Support read and write the same file at the same time
         with open('fruits.txt', 'a') as file:
             file.write('\nBanana\n')
    
  • Read and Write
with open(fruits.txt, a+) as file:
   file.write('Banana\n') # <= Cursor moves to the end of file
   file.seek(0) # <= Cursor moves to position 0
   content = file.read() # <= Cursor moves to the end of file
   file.write(content)
  • exercise 寫三次
with open('file.txt', 'a+') as r:
  r.seek(0)
  content = r.read()
  print(content)
  r.write('\n')
  r.write(content)
  r.write('\n')
  r.write(content)