Python Tutorial for Beginners - 9. File Objects - Reading and Writing to Files

Table of Contents

  1. Test text
  2. Run Code 1 in Python
  3. Run Code 2 in Python
  4. Run Code 3 in Python
  5. Run Code 4 in Python
  6. Run Code 5 in Python
  7. Run Code 6 in Python
  8. Run Code 7 in Python
  9. Run Code 8 in Python

Test text

1
2
3
4
5
6
7
8
9
10
1) This is a test file
2) With multiple lines of data...
3) Third line
4) Fourth line
5) Fifth line
6) Sixth line
7) Seventh line
8) Eighth line
9) Ninth line
10) Tenth line

text.txt

Run Code 1 in Python

1
2
3
4
5
6
7
8
9
10
#File Objects

##The Basics:
f = open("test.txt", "r")
f = open("test.txt", "w")
f = open("test.txt", "a")
f = open("test.txt", "r+")
print(f.name)
print(f.mode)
f.close()

text.txt

Run Code 2 in Python

1
2
3
##Reading Files:
with open("test.txt", "r") as f:
	pass

start.py

Run Code 3 in Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
##Reading Files: uncomment part by part 
with open("test.txt", "r") as f:
	#pass

	#Small Files:
	f_contents = f.read()
	print(f_contents)

	##Big Files:
	#f_contents = f.readlines()
	#print(f_contents)

    ###With the extra lines:
	#f_contents = f.readline()
	#print(f_contents)
	#f_contents = f.readline()
	#print(f_contents)

	###Without the extra lines:
	#f_contents = f.readline()
	#print(f_contents, end = '')
	#f_contents = f.readline()
	#print(f_contents, end = '')

	###Iterating through the file:
	#for line in f:
		#print(line, end = '')

	###Going Back....:
	#f_contents = f.read()
	#print(f_contents, end = '')

	###Printing by characters:
	#f_contents = f.read(100)
	#print(f_contents, end = '')
	#f_contents = f.read(100)
	#print(f_contents, end = '')
	#f_contents = f.read(100)
	#print(f_contents, end = '')

	###Iterating through small chunks:
	#size_to_read = 100
	#f_contents = f.read(size_to_read)
	#while len(f_contents) > 0:
		#print(f_contents)
		#f_contents = f.read(size_to_read)

	###Iterating through small chunks, with 10 characters:
	#size_to_read = 10
	#f_contents = f.read(size_to_read)
	#print(f_contents, end = '')
	#f.seek(0)
	#f_contents = f.read(size_to_read)
	#print(f_contents, end = '')
	#print(f.tell())
	#while len(f_contents) > 0:
		#print(f_contents, end = '*')
		#f_contents = f.read(size_to_read)
#print(f.mode)
#print(f.closed)
#print(f.read())

start.py

Run Code 4 in Python

1
2
3
4
5
6
7
8
9
10
11
12
##Writing Files:
###The Error:
with open("test.txt", "r") as f:
	f.write("Test")

###Writing Starts:
with open("test2.txt", "w") as f:
	pass
	f.write("Test")
	f.seek(0)
	f.write("Test")
	f.seek("R")

start.py

Run Code 5 in Python

1
2
3
4
5
##Copying Files:
with open("test.txt", "r") as rf:
	with open("test_copy.txt", "w") as wf:
		for line in rf:
			wf.write(line)

start.py

Run Code 6 in Python

1
2
3
4
5
6
#Copying the/your image:
###The Error
with open("yourPic.jpg", "r") as rf:
	with open("yourPic_copy.jpg", "w") as wf:
		for line in rf:
			wf.write(line)

start.py

Run Code 7 in Python

1
2
3
4
5
###Copying the image starts, without chunks:
with open("yourPic.jpg", "rb") as rf:
	with open("yourPic_copy.jpg", "wb") as wf:
		for line in rf:
			wf.write(line)

start.py

Run Code 8 in Python

1
2
3
4
5
6
7
8
###Copying the image with chunks:
with open("yourPic.jpg", "rb") as rf:
	with open("yourPic_copy.jpg", "wb") as wf:
		chunk_size = 4096
        rf_chunk = rf.read(chunk_size)
        while len(rf_chunk) > 0:
            wf.write(rf_chunk)
            rf_chunk = rf.read(chunk_size)

start.py