Important Notice:

File Handling in Python

File Handling in Python

16 views 1 min read

File HandlingFile ModesModeDescriptionrRead (default)wWrite (overwrite)aAppendxCreate new filerb, wbBinary modeReadingwith open("test.txt", "r") as f:\n content = f.read()\n print(content)Writingwith open("output.txt", "w") as f:\n f.write("Hello World\n")\n f.write("Python")Line by Linewith open("test.txt") as f:\n for line in f:\n print(line.strip())File Methods

  • read() — read entire file
  • readline() — read one line
  • readlines() — list of lines
  • write() — write string
  • writelines() — write list of strings