CS_3003 - Unit 5 - Files

About File Processing using Python related to http://j.mp/cs3003-unit5 Files and exception: text files, reading and writing files, format operator; command line arguments, errors and exceptions, handling exceptions, modules, packages; Illustrative programs: word count, copy file.

41 flashcards · Programming Languages · by rajasekaranap

  1. Open a file in append mode ( "a" ) creates a file if a file is not present If the file is present, it then adds (or appends) to the original contents of the file. The cursor is positioned at the end of the present data in the file.

    read, write and...then what is the third mode?

  2. A record consists of collection of data fields that conform to a previously defined structure that can be stored on or retrieved from a file.

    It is one of the two basic components (the other one being field) of a database.

  3. File Functions open() create a new file or open a existing file

    Function to open a file.

  4. Code Review: file = open("source.txt","r") data = file.read(); print(data) What it will do ?

    It reads and prints the contents of the file (source.txt)

  5. Files Basic A binary file is a collection of bytes.

    both start with letter 'b'

  6. A text file is a stream of characters that can be processed sequentially.

    It is in ASCII

  7. file = open("data.txt", "r") Here filename is the data.txt of the file to be opened and r is mode specifies the purpose of opening the file. file is the file object which will hold the reference to the opened (or created) file.

  8. READING and WRITING FILES Opening a file in "w" mode 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.

    True

  9. Snippet A // file_name = "data.txt "is a valid path to a file // mode = "r" is indicating file opened in read mode file = open(file_name,mode) data = file.read() print(data) Snippet A is used for read entire file content. True or False? Snippet B // file_name = "data.txt "is a valid path to a file // mode = "r" is indicating file opened in read mode file = open(file_name,mode) data = file.readLine() print(data) Snippet B is used for read a single line from a file. True or False?

    True True Snippet A is for writing to a binary file. Snippet B is for writing to a text file.

  10. What is default delimiter for readLines() method ?

    "\n" - new line

  11. A file is a collection of bytes stored on a secondary storage device, which is generally a disk of some kind. It is identified by a name, which is given at the time of its creation. It may be amended, moved from one storage device to another or removed completely when desired.

    An I/O asset of the operating system.

  12. seek() - It is used to move the reading control to different positions in the file. tell() - It tells the byte location of current position of cursor in file pointer.

    What are the functions required to control the file_position_indicator? They are required to perform random access of data in a file.

  13. There are two types of files. What are they?

    Text files and binary files.

  14. To process files, there are two types of access possible. What are they?

    Sequential access and Random access.

  15. Attempt the Sequential Access challenge available at http://j.mp/fileUpdate2C Make the correct changes to the code as indicated. What are the last 5 lines of the output?

    Reading contents of clients2.txt - 0 ashok 190 - 1 bala 200 - 2 junkie 0 - 3 mala 111

  16. Text Mode Description r Open an existing file for reading. r+ Open an existing file for update (reading and writing). w Create a file for writing. If the file already exists, discard the current contents. w+ Create a file for update. If the file already exists, discard the current contents. a, a+ Append: open or create a file for writing at the end of the file. Binary Mode Description rb opens a binary file in reading mode wb opens or create a binary file in writing mode ab, ab+ opens a binary…

    All possible modes in which files can be opened.

  17. A record consist of a collection of data fields that conforms to a previously defined structure that can be stored on or retrieved from a file.

    In random access, compound retrieval of data is attributed to this.

  18. You have been asked to code up a program to update the contents of an existing file. You prompt the user with the following question: "Do you want to overwrite the existing information in the file? (Yes / No)" If the user responds with "Yes", you will open the file (to be modified) using the write mode. If the user responds with "No", you will use the append mode.

    Choose between "read", "write" and "append"

  19. file =open("one.txt","r"); If one.txt file exists, file is a valid object. True or False?

    True.

  20. You have been asked to write a spell checker program to correct the spelling mistakes in a text file. To be efficient, which of the two processing methods will you use? A Sequential access (for reading only) B Sequential access (for both reading and writing) C Random access (for both reading and writing ) D Random access for reading and Sequential access for writing

    Choice B

  21. You have been asked to write a client accounts management program for a bank that will make updates to specific accounts several number of times in the course of the day. To be efficient, which of the two processing methods will you use? A Sequential access (for reading only) B Sequential access (for both reading and writing) C Random access (for both reading and writing ) D Random access for reading and Sequential access for writing

    Choice C

  22. The tmpfile() function is used to create a temporary file. The function opens the corresponding stream with access parameters set as “w+”. The file created with the said function will be automatically deleted when all references to the file are closed. That is, the file created will be automatically closed and erased when the program has been completely executed.

    Try it out on https://repl.it/@ashokb/tempFileCreation

  23. Attempt the Random Access challenge available at http://j.mp/fileUpdateC Make the correct changes to the code as indicated. What are the last 5 lines of the output?

    Validating update - 0 ashok 90 - 1 bala 100 - 2 junkie -10000 - 3 mala 11

  24. Random-access files are appropriate for airline reservation systems banking systems point-of-sale systems any kinds of transaction-processing systems that require rapid access to specific data.

    Opposite of "sluggish" or "slow"

  25. What are the various operations when it comes to file management?

    File management: It basically means all operations related to creating, renaming, deleting, merging, reading, writing, etc. of any type of files.

  26. Code Review: file = open("dest.txt","w") data = "Hello Python" file.write(data) What it will do ?

    It will write the contents of data to destination file (dest.txt)

  27. fileObject.read() is method that returns the file content as single string.

    file method

  28. fileObject.readline() is method that returns single line from a file.

    file method

  29. consider fileObject contains 100 characters ... fileObject.read (10) - It returns first 10 characters

    file method

  30. What if we pass negative value as size to read() method?

    It will return all the data

  31. fileObject.readlines() is method that returns list of strings from a file.

  32. If the source file is empty readlines() method will returns ?

    Empty List

  33. User wants to add some data to file but not ready to lose existing data. So we need to open file in append mode.

    file mode

  34. Files with the extension of JPG is binary type file.

    file type

  35. If need to write list strings to a file then we need to use wirtelines() method

  36. fileToRead = open("source.txt","r") data = fileToRead.readline() while data!="" print(data) data = fileToRead.readline()

    Complete the code

  37. If we used w to open the file then it will be emptied before the text will be inserted at the current file stream position, default 0.

    file mode

  38. The open() function takes two parameters; filename, and mode.

  39. It is a good practice to always close the file when you are done with it.

  40. If we need to current position of file pointer then we need to call tell() method.

  41. If we need to write the changes to the file before closing then we need to use call flush() method.