Skip to content

Commit 5101a73

Browse files
committed
final commit v1.0
0 parents  commit 5101a73

9 files changed

+82
-0
lines changed

empty.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
I love programming!
2+
I love Python!

example.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
i love python and i love programming

listofname.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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

read-line-by-line.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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

read-lines-in-a-list.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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())

read.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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)

write-append-to-file.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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("\n9. Yunus") # sample instance

write-read-example.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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)

write.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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

0 commit comments

Comments
 (0)