Q.1 what are files. Why do we need them?
Answer-
Files and Need of files-
1. A file is collection of data stored on a secondary storage device like hard disk.
2. Console oriented I/O operations pose two major problems-
a. Time consuming
b. Entire data lost when program terminate.
3. When program is being executed, its data is stored in RAM
4. RAM can accessed faster by CPU, but RAM is volatile/ not permanent storage
5. If you need data in future then you need to store data on permanent storage in files.
6. Files stored in name locations on storage media.
7. To use file first open file, read or write it and then finally close it.
8. File is used because real life applications involve large amounts of data.
Q.2 Differentiate between absolute path and relative path.
Answer-
Q.3 What are the types of files. or Explain Text file and Binary file.
Answer-
Text File-
1. It is stream of characters that can be sequentially processed by a computer in forward direction.
2. File open for read, write and append operations, but one at a time
3. Each line contains 255 characters
4. Each line ends with newline character ‘\n’
5. Each file ends with end of file marker(EOF)
6. Text files processed sequentially.
7. Example- 123 stored in 3 bytes
Binary File-
1. Binary file is a file which contains any type of data enclosed in binary form for computer storage and processing
purpose.
2. It includes files such as text, audio, video, images, pdfs, zip files.
3. Binary files also referred as a character stream.
4. Binary file does not require any special processing.
, 5. Binary file can be either processed sequentially randomly depending on the need of application.
6. Binary file does not required any special processing
7. Binary file also ends with end of file marker(EOF)
8. Example- 123 stored in 1 byte
Q. 4. Explain the utility of open() function.
Answer-
Open() function-
1. Before reading from or writing to file, you must first open file using built-in open() function.
2. Open() function creates a file object, which will be used to invoke methods associated with it.
3. Syntax for open() function-
Fileoobject=open(“filename”,”access_mode”)
4. Here filename is string value that specifies name of file that you want to access.
5. Access_mode indicates the mode in which file has to be opened.
i.e. read, write, append
6. The open function returns the file object.
7. This file object will be used to read write or perform any other operation on the file
8. File object works as file handle.
Example of opening file using open() function-
f=open(“file1.txt”,”w”)
f.write(“Keep learning python”)
f.close()
print(“Data written to te file”)
Q.5 What are different file access modes in which you can open a file?
Answer-
File Access Modes-
• Read(r, r+, rb, rb+)-read content from begining of file
1. r mode open text file for reading only
2. r+ mode open text file for read as well as write
3. rb mode open binary file for reading only
4. rb+ mode open bnary file for read as well as write
• Write (w, w+, wb, wb+)- write content from begining of file
1. w write mode, overwrite text file, create new if not exists.
2. W+ read and write mode, overwrite text file, create new file if not exists
3. wb write mode, overwrite binary file, create new if not exists.
4. wb+ read and write mode, overwrite binary file, create new file if not exists
• Append(a, a+, ab, ab+)-add content at the end begining of file
1. a append mode, add at end of text file, create new if not exists.
2. a+ append and read mode,add at end of text file, create new file if not exists
3. ab append mode, add at end of binary file, create new if not exists.
4. ab+ append and read mode, add at end of binary file, create new file if not exists.
Q.6 With the help of example explainany three attributes of file object.
Answer-
File Object Attributes-
1. Once the file successfully opened a file object is returned.
2. Using this file object you can easily access different types of information related to that file.
3. This information can be obtained by reading values of specific attributes of the file.
4. Following are the three file object attributes-
1. Fileobj.closed()- It returns True if file is closed ,otherwise returns False.
2. Fileobj.mode()- It returns file access mode in which file has been opened.
3. Fileobj.name()- It returns name of the file.
Example of file object attribute-