Skip to content

Commit 31e7c89

Browse files
authored
Python Read Text File
Python Read Text File
1 parent 38964b7 commit 31e7c89

File tree

6 files changed

+30
-0
lines changed

6 files changed

+30
-0
lines changed

read_all_text.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
f = open('test.txt', 'r')
2+
3+
print(f.read())
4+
5+
f.close()

read_file_loop.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
f = open('test.txt', 'r')
2+
3+
for line in f:
4+
print(line)
5+
6+
f.close()

read_readline.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
f = open('test.txt', 'r')
2+
3+
print(f.readline())
4+
print(f.readline())
5+
print(f.readline())
6+
7+
f.close()

read_unicode.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
f = open('test_unicode.txt', 'r', encoding='utf-8')
2+
3+
for line in f:
4+
print(line)
5+
6+
f.close()

test.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Hello! from toricode.com
2+
This file to test Python text file reading.
3+
Happy Coding!

test_unicode.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Việt Nam
2+
日本
3+
中国

0 commit comments

Comments
 (0)