Skip to content

Latest commit

 

History

History
16 lines (11 loc) · 856 Bytes

Read and write files.md

File metadata and controls

16 lines (11 loc) · 856 Bytes

BAIME banner

Reading and writing files

Simple commands in Python for writing in a text file

List of commands:

  • open(filename): returns a file object, you can assign the result to a variable, such as 'file'.
  • file.close(): Closes the file, just like 'File -> Save' in word for example.
  • file.read(): Reads the content of the file. You can assign the result to a variable.
  • file.readline(): Reads just one line of a text file.
  • file.truncate(): Empties the entire file, be carefull when using this on a file you wish to save.
  • file.write('stuff to write here or variables with input here'): Writes the 'stuff' you typed or variable you selected to the file.
  • file.seak(0): Moves the read / write location to the beginning of the file.