filmov
tv
Writing in a Text File Exercise - Copy Data, Convert to TITLE, Capitalize, Replace Char in Text File
![preview_player](https://i.ytimg.com/vi/jOwzed9ng4Q/maxresdefault.jpg)
Показать описание
In this tutorial we will solve Exercise on How to write in a Text File in Python
Exercise on File Handling :-
Copy data from one file to another
Read a file & write number of words in a another file
Read a file & write it in TITLE format in another file
Read a file & write each word in a separate line in another file
Copy data from one file to another and add line number to it
Copy data from one file to another and CAPATALIZE each line
Copy data from one file to another and replace alphabets with '*' , spaces with '_' and digits with '#'
Modes in File Handling :-
r: open an existing file for a read operation.
w: open an existing file for a write operation. If the file already contains some data then it will be overridden but if the file is not present then it creates the file as well.
a: open an existing file for append operation. It won’t override existing data.
r+: To read and write data into the file. The previous data in the file will be overridden.
w+: To write and read data. It will override existing data.
a+: To append and read data from the file. It won’t override existing data.
Opening file in python:-
open(path, mode) function opens the file with the specified path of file & opening mode
Reading from a Text File :-
read() function - reads complete file as a single string
readline(n) function - reads 1 one line at a time if 'n' is not passed, if 'n' is given reads 'n' characters from that line but does not go to second line
readlines() function - returns a list containing each line as an element of the list
Closing file in python:-
close() function closes the file & pushes/makes all the changes in the file.
Exercise on File Handling :-
Copy data from one file to another
Read a file & write number of words in a another file
Read a file & write it in TITLE format in another file
Read a file & write each word in a separate line in another file
Copy data from one file to another and add line number to it
Copy data from one file to another and CAPATALIZE each line
Copy data from one file to another and replace alphabets with '*' , spaces with '_' and digits with '#'
Modes in File Handling :-
r: open an existing file for a read operation.
w: open an existing file for a write operation. If the file already contains some data then it will be overridden but if the file is not present then it creates the file as well.
a: open an existing file for append operation. It won’t override existing data.
r+: To read and write data into the file. The previous data in the file will be overridden.
w+: To write and read data. It will override existing data.
a+: To append and read data from the file. It won’t override existing data.
Opening file in python:-
open(path, mode) function opens the file with the specified path of file & opening mode
Reading from a Text File :-
read() function - reads complete file as a single string
readline(n) function - reads 1 one line at a time if 'n' is not passed, if 'n' is given reads 'n' characters from that line but does not go to second line
readlines() function - returns a list containing each line as an element of the list
Closing file in python:-
close() function closes the file & pushes/makes all the changes in the file.
Комментарии