3a.file Hadling Online_text
3a.file Hadling Online_text
RAM
(RANDOM ACCESS
MEMORY/PRIMARY
MEMORY)
RAM
(RANDOM ACCESS
FILE MEMORY/PRIMARY
MEMORY)
HARD DISK
Need for a data file
The relative path is the path to some file with respect to your current working
directory (CWD).
For example:
Binary Files-A binary file stores the data in the same way as as stored in the memory. The .exe files,mp3 file,
image files are some of the examples of binary files. We can’t read a binary file using a text editor.e.g.
.bmp,.cdr etc.
Note: NO need to close the file explicitly as the resources get free as soon as the execution of with block gets over
File opening modes (for reading)
If you just give the filename without its path (e.g., just
“a.txt” rather than “c:/python37/file handling/a.txt”),
python will open or create the file in the same directory
in which the module file is stored
Always Close a text file to free the resources i.e. memory
close(): Used to close an open file. After using this method,an opened file will be
closed and a closed file cannot be read or written any more.
Sample code:
#file opened in append mode
f = open("a.txt", 'a+')
#checking if file is open
print(f.closed) #checking if the file is closed
print("Name of the file is",f.name)
f.close() #to close text file
print(f.closed) #checking if the file is closed
OUTPUT Note:
False closed is an attribute of
Name of the file is a.txt file object whereas
True close() is a
method/function
Writing onto a text file :
f=open("c:/python37/file handling/a.txt",'w')
f.write("hello\nhow are \tyou")
f.close()
Writing onto a text file :
strip() function
removes the leading
and trailing white
spaces(new line
character/space
Back
Program to read content of text file “notes.txt” and display content of a file using open()
method. [example of file as an iterator]
Back
Program to read content of text file “try.txt” and display content of a file character by character
using open() method. [example of file as an iterator]
h
file_as_iterator_ch() o
w
a
r
e
you
y
o Back
u
Program to read content of text file “try.txt” and display content of a file word by word using
open() method. [example of file as an iterator]
file_as_iterator_word()
Back
3. Program to count number of vowels in notes.txt
Back
4. Program to display # after every word from the file notes.txt
Back
5. Program in python to type text in a file till ~ is pressed as rightmost character
Back
6. Program in python to read first 2 lines from text file
Back
7. Program in python to append text at the end of text file
Back
8. Program in python to read line by line from text file
Back
9. Program in python to read entire file in a list
Back
10. Program in python to display each word in new separate line
Back
11. Program in python to count no. of words in a text file
Back
12. Program in python to show word with maximum length from a text file
Back
13. Program in python to display frequency of each word in a text file
Back
14. Program in python to store list elements (having days of the week) in a file and read
these contents from file again
Back
15. Python program to combine each line from first file with the corresponding line in second file
Back
16. Python Program to Copy the Contents of One File into Another (notes to
notes3)
Back
17. Python Program to Search for a word in text file and print part of line[searching for flying
in notes.txt]
Back
18. Python Program to read character by character from a text file
Back
19. Python Program to convert file contents upper to lower and lower to upper case in the
same opened file.
Back
20. WAP to find the lines in a text file that start with a digit/number.
Back
21. WAP to print the last line of a text file
Back
22. WAP to print the longest line of a text file
Back
23. WAP to copy the lines that start with Capital letters of a text file and paste it to a new file.
Back
24. WAP that will read a text file notes.txt and displays all lines not starting with ‘h’
Back
25. WAP that will read a text file notes.txt and will copy all those words whose length is four or more to
another file info.txt
Back
26. WAP that will take a user defined file name. The program should copy all the data from this file to
another file copy.txt.
Back
27. WAP to copy all the words starting with ‘F’ from the file to another text file info.txt.
Back
28. WAP to copy all file lines that start with a lowercase letter from the notes.txt file into info.txt
Back
Text file questions
SNO QUESTION
29. A text file contains alphanumeric text(say notes.txt). Write a program that read this text file and sends
only the numbers from this file to other file.
30. WAP in PYTHON to display the last 3 characters of a text file “NOTES4.TXT’.
31. Consider the following code:
ch = "A"
f=open("data.txt",'a')
f.write(ch)
print(f.tell())
f.close()
What is the output if the file content before the execution of the program is the string "ABC"? (Note that“
" are not the part of the string)
32. Consider the following code:
ch = "A"
f=open("data.txt",’w')
f.write(ch)
print(f.tell())
f.close()
What is the output if the file content before the execution of the program is the string "ABC"? (Note that"
" are not the part of the string)
29. A text file contains alphanumeric text(say notes.txt). Write a program that read this text file and sends
only the numbers from this file to other file
Back
30. WAP in PYTHON to display the last 3 characters of a text file “NOTES4.TXT’.
Back
31. Consider the following code:
ch = "A"
f=open("data.txt",'a')
f.write(ch)
print(f.tell())
f.close()
What is the output if the file content before the execution of the program is the string "ABC"? (Note that" " are
not the part of the string)
Back
32. Consider the following code:
ch = "A"
f=open("data.txt",’w')
f.write(ch)
print(f.tell())
f.close()
What is the output if the file content before the execution of the program is the string "ABC"? (Note that" " are
not the part of the string)
Back
Text file questions
SNO QUESTION
33. Assuming that a text file named TEXT1.TXT already contains some text written into it, write a function named
vowelwords(), that reads the file TEXT1.TXT and creates a new file named TEXT2.TXT, which shall contain only
those words from the file TEXT1.TXT which don’t start with an uppercase vowel (i.e., with ‘A’, ‘E’, ‘I’, ‘O’, ‘U’). For
example, if the file TEXT1.TXT contains Carry Umbrella and Overcoat When It rains
Then the text file TEXT2.TXT shall contain Carry and When rains
34. Assuming that a text file named TEXT1.TXT already contains some text written into it, write a function named
vowelwords(), that reads the file TEXT1.TXT and creates a new file named TEXT2.TXT, which shall contain only
those words from the file TEXT1.TXT which start with an uppercase vowel (i.e., with ‘A’, ‘E’, ‘I’, ‘O’, ‘U’). For
example, if the file TEXT1.TXT contains Carry Umbrella and Overcoat When It rains
Then the text file TEXT2.TXT shall contain Umbrella Overcoat It
35. Write a function in PYTHON to count the number of lines ending with a vowel from a text file “notes.TXT’.
36. Write a function to count and display the number of blanks present in a text file named “NOTES2.TXT”.
37. Write a function in PYTHON to count and display the number of words starting with alphabet ‘A’ or ‘a’ present in
a text file “lines.TXT”. Example:
If the file “LINES.TXT” contains the following lines,
A boy is playing there. There is a playground.
An aeroplane is in the sky.
Are you getting it?
The function should display the output as 5.
33. Assuming that a text file named TEXT1.TXT already contains some text written into it, write a function named
vowelwords(), that reads the file TEXT1.TXT and creates a new file named TEXT2.TXT, which shall contain only those
words from the file TEXT1.TXT which don’t start with an uppercase vowel (i.e., with ‘A’, ‘E’, ‘I’, ‘O’, ‘U’). For example,
if the file TEXT1.TXT contains Carry Umbrella and Overcoat When It rains
Then the text file TEXT2.TXT shall contain Carry and When rains
Back
34. Assuming that a text file named TEXT1.TXT already contains some text written into it, write a function named
vowelwords(), that reads the file TEXT1.TXT and creates a new file named TEXT2.TXT, which shall contain only those
words from the file TEXT1.TXT which start with an uppercase vowel (i.e., with ‘A’, ‘E’, ‘I’, ‘O’, ‘U’). For example, if the
file TEXT1.TXT contains Carry Umbrella and Overcoat When It rains
Then the text file TEXT2.TXT shall contain Umbrella Overcoat It
Back
35. Write a function in PYTHON to count the number of lines ending with a vowel from a text file “notes.TXT’.
Back
36. Write a function to count and display the number of blanks present in a text file named “NOTES2.TXT”.
Back
37. Write a function in PYTHON to count and display the number of words starting with alphabet ‘A’ or ‘a’ present in a text file
“lines.TXT”. Example: If the file “LINES.TXT” contains the following lines,
A boy is playing there. There is a playground.
An aeroplane is in the sky.
Are you getting it?
The function should display the output as 5.
Back
Text file questions
SNO QUESTION
38. Write a function show(n) that will display the nth character from the existing file “LINES.TXT”. If the total
characters present are less than n, then it should display “invalid n”.
39. Write a function in PYTHON that counts the number of “is” or “in” words present in a text file “LINES.TXT”.
40. Write a function EUCount() in PYTHON, which should read each character of a text file IMP.TXT, should count and
display the occurrences of alphabets E and U (including small cases e and u too).
41. Write a function in Python which accepts two text file names as parameters. The function should copy the
contents of first file into the second file in such a way that all multiple consecutive spaces are replaced by a
single space.
42. Write a function in Python to input a multi-line string from the user and write this string into a file named
‘Story.txt’. Assume that the file has to be created.
43. Write a program to display the last line of a text file.
44. Write a Python function to read and display a text file ‘notes.TXT'. At the end display number of lowercase
characters, uppercase characters, and digits present in the text file.
45. Write a Python function to display the size of a text file after removing all the white spaces (blanks, tabs, and
EOL characters).
46. Write a python program to read last 2 lines of a text file.
47. Write a python program to print from line 2 to line 5 (assuming the file has more than 5 lines)
38. Write a function show(n) that will display the nth character from the existing file “LINES.TXT”. If the total characters present
are less than n, then it should display “invalid n”.
Back
39. Write a function in PYTHON that counts the number of “is” or “in” words present in a text file “LINES.TXT”.
Back
40. Write a function EUCount() in PYTHON, which should read each character of a text file IMP.TXT, should count and display the
occurrences of alphabets E and U (including small cases e and u too).
Back
41. Write a function in Python which accepts two text file names as parameters. The function should copy the contents of first
file into the second file in such a way that all multiple consecutive spaces are replaced by a single space.
Back
42. Write a function in Python to input a multi-line string from the user and write this string into a file named ‘Story.txt’. Assume
that the file has to be created.
Back
43. Write a program to display the last line of a text file.
Back
44. Write a Python function to read and display a text file ‘notes.TXT'. At the end display number of lowercase characters,
uppercase characters, and digits present in the text file.
Back
45. Write a Python function to display the size of a text file after removing all the white spaces (blanks, tabs, and EOL
characters).
Back
46. Write a python program to read last 2 lines of a text file.
Back
47. Write a python program to print from line 3 to line 6 (assuming the file has more than 5 lines)
Back
Text file questions
SNO QUESTION
48. Write a python program to insert a new line at the beginning of the file
49. Write a python program to move the contents of a file to an list
50. What Will Be the Output of the Following Code Snippet?
fo = open("myfile.txt", "w+")
print ("File name is : ", fo.name)
seq="File handling is easy in python"
fo.writelines(seq)
fo.seek(0,0)
for line in fo:
print (line)
fo.close()
51. What Will Be The Output Of The Following Code Snippet?
fo = open("a.txt", "w+")
print ("File name is : ", fo.name)
txt = "This is 1st line,"
fo.writelines( txt )
seq = " This is 2nd line, This is 3rd line"
fo.seek(0, 2)
fo.writelines( seq )
fo.seek(0,0)
48. Write a python program to insert a new line at the beginning of the file
Back
49. Write a python program to move the contents of a file to an list
Back
50. Predict the output
Back
51. Predict the output
Back
Text file questions
SNO QUESTION
52. The following sample file called studentmarks.txt contains one line for each student in an imaginary class. The
students name is the first thing on each line, followed by some exam scores. The number of scores might be
different for each student.
Mohak 10 15 20 30 40
Manish 23 16 19 22
Ria 8 22 17 14 32 17 24 21 2 9 11 17
Joy 12 28 21 45 26 10
Freya 14 32 25 16 89
Using the text file studentmarks.txt write a program that prints out the names of students that have more than
five quiz scores.
53. Write a Python Program that Reads a Text File and Counts the Number of Times each character appears in the
Text File
54. Write a Python Program that Reads a Text File and Counts the Number of Times a Certain Letter Appears in the
Text File
55. Write a Python Program to Read a File and Capitalize the First Letter of Every Word in the File.
56. Write a Python Program to Read the Contents of a File in Reverse order i.e. your program should read last
line..then second last line and so on till the first line
52. The following sample file called studentmarks.txt contains one line for each student in an imaginary class. The students
name is the first thing on each line, followed by some exam scores. The number of scores might be different for each student.
Mohak 10 15 20 30 40
Manish 23 16 19 22
Ria 8 22 17 14 32 17 24 21 2 9 11 17
Joy 12 28 21 45 26 10
Freya 14 32 25 16 89
Using the text file studentmarks.txt write a program that prints out the names of students that have more than five quiz scores.
Back
53. Write a Python Program that Reads a Text File and Counts the Number of Times each character appears in the Text File
Back
54. Write a Python Program that Reads a Text File and Counts the Number of Times a Certain Letter Appears in the Text File
Back
55. Write a Python Program to Read a File and Capitalize the First Letter of Every Word in the File.
Back
56. Write a Python Program to Read the Contents of a File in Reverse order i.e. your program should read last line..then second
last line and so on till the first line
Back
RANDOM ACCESS METHODS
RANDOM ACCESS METHODS
seek method
tell method
seek method
fileobject.seek(offset [, from_what])
here offset is used to calculate the
position of fileobject in the file in bytes. Offset
is added to from_what (reference point) to get
the position. Following is the list of from_what
values:
seek method
and so on..
tell method
It's syntax is
fileobject.tell()
tell() method
tell() method