Grade 12 Exam  >  Grade 12 Notes  >  Computer Science for Grade 12  >  Chapter Notes: File Handling

File Handling Chapter Notes | Computer Science for Grade 12 PDF Download

File Handling


Revision Notes

  • Files are used to store huge collection of data and records permanently.
  • File can be stored in two ways: 
    • Text file store information in ASCII or unicode characters. In text file, each line of text is terminated, (delimited) with a special character known as EOL (End of line) character.
    • Binary file are just a file that contains information in the same format in which the information is held in memory, i.e. the file contain that is returned to you is raw. In binary file, there is no delimiter for a line.
  • Access modes specify the type of operations to be performed on the opened file.
  • read(), readline() and readlines() methods are available for reading data from the file.
  • write() and writelines() are used for writing data in the file.
  • pickle module is used in serialization of data. This allows us to store data in binary form in the file.
  • dump and load functions are used to write and read data from file.
  • The o pen() function creates a file object which would be utilized to call other methods associated with it.
  • Syntax : file object=open(filename[ access_mode],[ buffering]) Here is the parameter details: Ø filename: The file_name argument is a string value that contains the name of the file that you want to access.
  • access_mode: The access_mode determines the mode in which the file has to be opened i.e., read, write, append, etc. A complete list of possible values is given below in the table. This is optional parameter and the default file access mode is read (r).

File Opening Modes
File Handling Chapter Notes | Computer Science for Grade 12File Handling Chapter Notes | Computer Science for Grade 12

  • Buffering: If the buffering value is set to 0, no buffering will take place. If the buffering value is 1, line buffering will be performed while accessing a file. If you specify the buffering value as an integer greater than 1, then buffering action will be performed with the indicated buffer size. If negative, the buffer size is the system default (default behaviour).
  • The file object attributes: Once a file is opened and you have one file object, you can get various information related to that file. Here is a list of all attributes related to file object:
    File Handling Chapter Notes | Computer Science for Grade 12
  • file () : This is same as open ().
  • Random Access : There are two functions that allow us to access a file in a non-sequential or random mode.
    • tell() : It tells us the position of the file pointer.
    • seek() : It moves the file pointer to the position specified.
  • Functions
    (a) read () :
    syntax: <file handle>.read([n])
    It reads at most n bytes and returns the read bytes as string. If `n’ is not specified it reads the entire file.
    (b) readline () : syntax: <file handle>.readline ([n]) It reads a line of input, and returns it in the form of a string.
    (c) readlines () : syntax: <file handle>.readlines () It reads all lines and returns them in a list.
    (d) write () : syntax: <filehandle>.write (str1) It writes string str1 to file referenced by <file handle>
    (e) writelines () : syntax: <file handle>.writelines (L). It writes all strings in list L as lines to file referenced by <file handle>
    (f) flush () : syntax: <file object>.flush() It forces the writing of data on disc that is still pending in output buffer.
    (g) Importing sys module lets you read/write from the standard input/output device using sys.stdin.read () and sys.stdout.write().
    (h) split () function splits a line in columns. It returns columns as items of a list.
    (i) rename () function is used to rename a file existing on the disk. syntax: os.remane(<current_file_name>,<new_file_name>)
    (j) remove () function is used to delete a file existing on the disk. syntax: os.remove(<file_name>)
    (k) os.path.join () is used to assemble directory names into a path.
    (l) os.path.split () is used to splits off the last path component.
    (m) os.path.splittext() is used to split file name into primary name and extension.
    (n) os.path.exists () function check if a path actually exists.
The document File Handling Chapter Notes | Computer Science for Grade 12 is a part of the Grade 12 Course Computer Science for Grade 12.
All you need of Grade 12 at this link: Grade 12
1 videos|25 docs|18 tests

Top Courses for Grade 12

1 videos|25 docs|18 tests
Download as PDF
Explore Courses for Grade 12 exam

Top Courses for Grade 12

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Viva Questions

,

Sample Paper

,

File Handling Chapter Notes | Computer Science for Grade 12

,

Extra Questions

,

shortcuts and tricks

,

File Handling Chapter Notes | Computer Science for Grade 12

,

MCQs

,

Exam

,

past year papers

,

Previous Year Questions with Solutions

,

study material

,

Summary

,

practice quizzes

,

ppt

,

Important questions

,

Objective type Questions

,

Free

,

mock tests for examination

,

Semester Notes

,

File Handling Chapter Notes | Computer Science for Grade 12

,

pdf

,

video lectures

;