File tree 9 files changed +82
-0
lines changed
9 files changed +82
-0
lines changed Original file line number Diff line number Diff line change
1
+ I love programming!
2
+ I love Python!
Original file line number Diff line number Diff line change
1
+ i love python and i love programming
Original file line number Diff line number Diff line change
1
+ 1. Agung
2
+ 2. Aushafy
3
+ 3. Rizki
4
+ 4. Ardan
5
+ 5. Hady
6
+ 6. Danny
7
+ 7. Ayyub
8
+ 8. Fahras
9
+ 9. Yunus
Original file line number Diff line number Diff line change
1
+ """
2
+ Previously, we have been make program that can be open and read entire of file.
3
+ in this capter, we will make another program that can be read file's content line-by-line.
4
+ """
5
+ filename = 'listofname.txt' # select file by type file name
6
+ with open (filename ) as file_obj : # open file with open() function and make alias name from filename (file_obj)
7
+ for line in file_obj : # using for looping to read entire content line-by-line
8
+ print (line .rstrip ()) # we used rstrip() funtion to remove trailing spaces. it makes contents readable
Original file line number Diff line number Diff line change
1
+ """
2
+ Read from a file (Cont.) is a method to storing the lines in a list.
3
+ """
4
+ filename = 'listofname.txt' # select file by type file name
5
+ with open (filename ) as file_obj :
6
+ lines = file_obj .readlines ()
7
+
8
+ for line in lines :
9
+ print (line .rstrip ())
Original file line number Diff line number Diff line change
1
+ """
2
+ Reading an entire file at once.
3
+
4
+ Algorithm: Open file with open() function and then read content of the file with read() function
5
+ """
6
+
7
+ filename = 'listofname.txt' # select file
8
+
9
+ with open (filename ) as file_obj : # open file with open() function
10
+ content = file_obj .read () # read entire content of file with read() function
11
+
12
+ print (content )
Original file line number Diff line number Diff line change
1
+ """
2
+ another write program function to add some text in the file which has the content before.
3
+ """
4
+ filename = "listofname.txt" # type file name which you want to add some text inside that
5
+ with open (filename , "a" ) as file_obj : # open() function with 'a' argument to appending text in file which has content
6
+ file_obj .write ("\n 9. Yunus" ) # sample instance
Original file line number Diff line number Diff line change
1
+ """
2
+ This program was made to write-and-read data in some file.
3
+ User is allowed to input some text inside some file and check wheter the text already write successfully.
4
+
5
+ Algorithm:
6
+ 1. User input file name that will be selected for the write-operation.
7
+ 2. After that, we will make sure that file was successfully opened
8
+ 3. If the file already successfully opened. next, user can input some text
9
+ 4. If user input some text successfully, it will be automatically writes in file which selected before.
10
+ 5. Last, checking wheter text is successfully write inside the text or nope.
11
+ """
12
+
13
+ filename = input ('Select file that you want to write-read: ' ) # user should select some file
14
+ with open (filename , 'w' ) as file_object : # open file function with 'w' argument to allows write some text
15
+ print ('File has been opened successfully!\n ' ) # if file successfully opened, this program will be shown
16
+ text = input ('Text that you want to add: ' ) # if file has been successfully opened.
17
+ # it will be allowed user to input some text
18
+ file_object .write (text ) # write function to write text to file
19
+
20
+ # make sure that we have successfully add some text inside that file with read function
21
+ with open (filename ) as read_object : # again, we open the file
22
+ content = read_object .read () # read function to check wheter there is any text inside that file
23
+
24
+ if text in content : # check wheter text already added successfully.
25
+ print ('You have been successfully to add text to your file!' ) # if text successfully added. print text!
26
+ print ('Your text : ' + text )
Original file line number Diff line number Diff line change
1
+ """
2
+ Simple program to write an instance to a file.
3
+
4
+ open() argument: 'w' to write in empty file, 'a' to appending to a file.
5
+ """
6
+ filename = 'empty.txt' # select file that you want to add some text inside.
7
+ with open (filename , 'w' ) as file_obj : # open function with 'w' argument it is mean you will add some text in empty file
8
+ file_obj .write ("I love programming!\n " ) # write() function to writes some text inside files
9
+ file_obj .write ("I love Python!\n " ) # to write multiple lines with \n in the end of line
You can’t perform that action at this time.
0 commit comments