File Handling

Cards (30)

  • Types of File include Text Files and Binary Files.
  • File Object Attributes include fileObj.closed which returns true if file is closed otherwise false, fileObj.mode which returns the access mode, and fileObj.name which returns the name of the file.
  • The write() method is used to write a string to an already opened file and does not add a new line character to the end of the string.
  • The write() method returns nothing.
  • The fileObj.write(string) method is used to write a string to an already opened file.
  • The writeline() method is used to write a list of strings to an already opened file.
  • The read() method is used to read a string from an already opened file and takes a count as an optional parameter.
  • In read() method, if the count is missing or has a negative value, then it reads the entire contents of the file.
  • The read method returns newline as '\n'.
  • The readline() method is used to read a single line from the file and returns an empty string when the end of file has been reached.
  • A blank line in a file also has an '\n' character, therefore the readline() method will print a blank line at that place.
  • r - opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
  • rb - opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
     
  • r+ - opens a file for both reading and writing. The file pointer placed at the beginning of the file.
  • rb+ - opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file.
  • w - opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
     
  • wb - opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
  • wb+ - opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
     
  • a - opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
  • ab - opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
  • a+ - opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
  • ab+ - opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
  • fileObj.closed - Returns true if file is closed otherwise false
  • fileObj.mode - Returns the access mode
  • fileObj.name - Returns the name of the file
  • w+ - opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
  • The write() method is used to write a string to an already opened file. This string may include numbers, special characters, or other symbols.
  • writeline() method - Used to write list of strings
  • read() method - This method is used to read a string from an already opened file.
  • write() method returns nothing