Showing posts with label File Handling. Show all posts
Showing posts with label File Handling. Show all posts

File Handling Test 6

 File Handling Test 6

Q1. Do we need to use the close() function when we use 'with' statement to open a file?

Q2. Which mode you will choose for the following situations:

1. For reading file

2. For creating new file if not existing

3. All previous data over written by new data.

4. All new data added at the end of the file

Q3. Write the code to open a file "try.txt" in write mode by using 'with' statement.

Q4. When we open a file in append mode. Write T/F for the following statements

  1. New file created, if file does not exist.
  2. Error comes, if file does not exist.
  3. File open with no data, if file already exist and had some data.
  4. New data will over wrie the existing data.
  5. New data will add at the end of the existing data.

Q5. a ) Write a program to add the following data in a file named "data.txt"
                        Welcome to my Blog
                        This blog is for python learners

Q6. Write the output, if you execute the following program twice.

        f = open("data.txt",'a')
        f.write("Welcome to my Blog\n")
        f.write("This blog is for python learners")
        f.close()

Q7. Write the following code in one statement:
f = open("data.txt",'r')
f.read(10)

Q8. Write a program to read and display three lines(line by line) from  file "data.txt"

Q9. Write the output if the file contains the following lines :

Welcome to my Blog
This blog is for python learners
This blog is for python learners


a)
f=open("data.txt", "r")
print(f.readline( ))
print(f.readline( ))
print(f.readline( ))

b)
f=open("data.txt", "r")
print(f.readline( )end=' ')
print(f.readline( )end=' ')
print(f.readline( )end=' ')


Q10. Write a program to read entire file "data.txt" line by line.


 SOLUTIONS

Q1. Do we need to use the close() function when we use 'with' statement to open a file?

Ans. No

Q2. Which mode you will choose for the following situations:

1. For reading file

2. For creating new file if not existing

3. All previous data over written by new data.

4. All new data added at the end of the file

Ans. 

1. Read mode

2. Write mode/Append mode

3. Write mode

4. Append mode

Q3. Write the code to open a file "try.txt" in write mode by using 'with' statement.

Ans. with open("try.txt" , 'w') as f1

Q4. When we open a file in append mode. Write T/F for the following statements

  1. New file created, if file does not exist.
  2. Error comes, if file does not exist.
  3. File open with no data, if file already exist and had some data.
  4. New data will over wrie the existing data.
  5. New data will add at the end of the existing data.
Ans.
  1. True
  2. False
  3. False
  4. False
  5. True
Q5. a ) Write a program to add the following data in a file named "data.txt"
                        Welcome to my Blog
                        This blog is for python learners

Ans. f = open("data.txt",'a')
        f.write("Welcome to my Blog\n")
        f.write("This blog is for python learners")
        f.close()

Q6. Write the output, if you execute the following program twice.

        f = open("data.txt",'a')
        f.write("Welcome to my Blog\n")
        f.write("This blog is for python learners")
        f.close()
Ans
Welcome to my Blog
This blog is for python learnersWelcome to my Blog
This blog is for python learners

Q7. Write the following code in one statement:
f = open("data.txt",'r')
f.read(10)

Ans. open("data.txt",'r').read(10)

Q8. Write a program to read and display three lines(line by line) from  file "data.txt"
Ans.
f=open("data.txt", "r")
print(f.readline( ))
print(f.readline( ))
print(f.readline( ))

Q9. Write the output if the file contains the following lines :

Welcome to my Blog
This blog is for python learners
This blog is for python learners


a)
f=open("data.txt", "r")
print(f.readline( ))
print(f.readline( ))
print(f.readline( ))

b)
f=open("data.txt", "r")
print(f.readline( )end=' ')
print(f.readline( )end=' ')
print(f.readline( )end=' ')

Ans a)
Welcome to my Blog

This blog is for python learners

This blog is for python learners

b)
Welcome to my Blog
This blog is for python learners
This blog is for python learners

Q10. Write a program to read entire file "data.txt" line by line.
Ans.
f = open("data.txt", "r")
line=f.readlines( )
for i in line:
    print(i, end = ' ')
f.close( )

Computer Science Class XII Python File Handling Test Series Part 5


Computer Science Class XII Python File Handling Test Series Part 5

Q1. Reading and writing opeation can be done after close() function(T/F)

Q2.         f = open("test.txt", "r")

Write the output f the following

1. print(f.name)

2. print(f.mode)

3. print(f.closed)

4. prin(f.readable())

Q3. Write a program to accept five numbers from the user and write in a file "num.txt"

Q4. What type of error is shown by the following line of code.

                f.write(3)

where f is file handle associated with text file named "try.txt"

Q5. write() function takes integer argument. (T/F)

Q6. Write a proram to accept 10 numbers from the user. if the number is even than write that number in "even.txt", otherwise, write in file "odd.txt"

Q7. Write a program to create a list of five subjects and write them in a file named "subject.txt"


Q8. What type of error is shown by the following line of code? 

    f.write(try)

Where f is file object and try is a an object of list type.

Q9. What type of error is shown by the following line of code? 

    f.writeline(try)

Where f is file object and try is a an object of list type.

Q10. writelines() method does not add any EOL character to the end of String. (T/F)


SOLUTIONS

Q1. Reading and writing opeation can be done after close() function(T/F)

Ans. False

Q2.         f = open("test.txt", "r")

Write the output f the following

1. print(f.name)

2. print(f.mode)

3. print(f.closed)

4. prin(f.readable())

Ans. 

1. test.txt

2. r

3. False

4. True

Q3. Write a program to accept five numbers from the user and write in a file "num.txt"

Ans.

f=open("num.txt","w")
for i in range(5):
n=int(input("Enter number"))
f.write(str(n))
f.close()


Q4. What type of error is shown by the following line of code.

                f.write(3)

where f is file handle associated with text file named "try.txt"

Ans. TypeError

Q5. write() function takes integer argument. (T/F)

Ans. False

Q6. Write a proram to accept 10 numbers from the user. if the number is even than write that number in "even.txt", otherwise, write in file "odd.txt"

Ans. 

f=open("num.txt","w")
f1=open("odd.txt","w")
for i in range(10):
n=int(input("Enter number"))
if n%2==0:
f.write(str(n))
f.write("\n")
else:
f1.write(str(n))
f.write("\n")
f.close()
f1.close()

Q7. Write a program to create a list of five subjects and write them in a file named "subject.txt"

f=open("num.txt","w")
sub=["Englsh\n","Hindi\n","Math\n","Science\n",
"SST\n"]
f.writelines(sub)
f.close()

Q8. What type of error is shown by the following line of code? 

    f.write(try)

Where f is file object and try is a an object of list type.

Ans. TypeError: write() argument must be str, not list

Q9. What type of error is shown by the following line of code? 

            f.writeline(try)

Where f is file object and try is a an object of list type.

Ans. AttributeError

Q10. writelines() method does not add any EOL character to the end of String. (T/F)

Ans. True








Computer Science Class XII Python File Handling Test Series Part 4

Computer Science Class XII Python File Handling Test Series Part 4


Q1. Write a program in python to read entire content of file ("data.txt")

                                 

Q2. Write a program in python to read first 5 characters from the file("data.txt") 

                            

Q3. Write a program in python to read first line from the file("data.txt")

                               

Q4. Write a program in python to display number of lines in a file("data.txt"). 

                             

Q5. Write a program in python to display first line from the file("data.txt") using readlines(). 

Q6. readlines() function returns the entire data in the form of list of _____

Q7. Name two functions which are used to write data into file.

Q8. Accept five names from the user and write in a file "name.txt"

Q9. Accept five sports names from the user and write in a file "sport.txt" (each name should write in separate line)

Q10. Accept five hobbies from the user and write in a file "hobby.txt" (each hobby should write in separate line) without using write() function.


SOLUTIONS

Q1. Write a program in python to read entire content of file ("data.txt")


                                 

Q2. Write a program in python to read first 5 characters from the file("data.txt") 

                                

Q3. Write a program in python to read first line from the file("data.txt")

                                 

Q4. Write a program in python to display number of lines in a file("data.txt"). 

                                

Q5. Write a program in python to display first line from the file("data.txt") using readlines(). 

                                

Q6. readlines() function returns the entire data in the form of list of _____

Ans. String

Q7. Name two functions which are used to write data into file.

Ans. write() and writelines()

Q8. Accept five names from the user and write in a file "name.txt"

Ans.

f = open("name.txt","w")

for i in range(5):

   n = input("Enter name")

   f.write(n)

f.close()

Q9. Accept five sports names from the user and write in a file "sport.txt" (each name should write in separate line)

f = open("name.txt","w")

for i in range(5):

       n = input("Enter sport name")

       f.write(n)

       f.write('\n')

f.close()

Q10. Accept five hobbies from the user and write in a file "hobby.txt" (each hobby should write in separate line) without using write() function.

Ans.

f = open("hobby.txt","w")

h = [ ]

for i in range(5):

       n = input("Enter hobby name")

       h.append(n,'\n')

f.writelines(h)

f.close()

Computer Science Class XII Python File Handling Test Series Part 3

Computer Science Class XII Python File Handling Test Series Part 3

Q1. What is the difference between r+ and w+ mode?

Q2. What is the difference between write and append mode?

Q3. Which method is used to close the file in python?

Q4. Write the statement to close the file "test.txt" which is associated with file object named "fo".

Q5. Write one similarity and one difference between a+ and w+.

Q6. What is the difference between read() and read(n) functions?

Q7. What is the difference between readline() and readlines() ?

Q8. Write a program to read first 10 characters from a file named "data.txt"

Q9. Write a program to read entire content from the file named "test.txt"

Q10. Write the output of the following, if the data stored in file "try.txt" is given below. ("f" is the file object associated with file)

                                Try Try but never cry

    print(f.read(4))

    print(f.read(5))

    print(f.read())


SOLUTIONS

Q1. What is the difference between r+ and w+ mode?

Ans. r+ mode will return error if file does not exist while w+ mode will create a new file if file does not exist.

Q2. What is the difference between write and append mode?

Ans. Write mode over writes the existing data while append mode add the new data at the end of the file.

Q3. Which method is used to close the file in python?

Ans. close()

Q4. Write the statement to close the file "test.txt" which is associated with file object named "fo".

Ans. fo.close()

Q5. Write one similarity and one difference between a+ and w+.

Ans. Similarity : In both the modes, we can do read and write operations.

Difference : In w+ mode file will be truncated (previous data lost) while in a+ mode, file's existing data will not be deleted and new data will be added at the end of the file.

Q6. What is the difference between read() and read(n) functions?

Ans. read() : will read entire data from the file.

        read(n) : will read first n characters/bytes from the file.

Q7. What is the difference between readline() and readlines() ?

Ans. readline() :This function will read one line from the file.

        readlines() : This function will read all the lines from the files.

Q8. Write a program to read first 10 characters from a file named "data.txt"

Ans. 

f = open("data.txt","r")

data = f.read(10)

print(data)

Q9. Write a program to read entire content from the file named "test.txt"

Ans. 

f = open("test.txt, "r")

d = f.read()

print(d)

Q10. Write the output of the following, if the data stored in file "try.txt" is given below. ("f" is the file object associated with file)

                                Try Try but never cry

    print(f.read(4))

    print(f.read(5))

    print(f.read())

Ans.

Try

Try b

ut never cry

Most Recently Published

File Handling Test 6

 File Handling Test 6 Q1 . Do we need to use the close() function when we use 'with' statement to open a file? Q2. Which mode you wi...

CS - IP Assignment/Worksheet