Welcome to part 8 of my Learn to Program series. This time we’ll cover how to read and write files. I’ll answer a question I received about recursive functions. We’ll investigate what a Tuple is. And, of course we’ll work our way through problems so we can get better at solving programming problems.

All of the code follows the video below. It is heavily commented and is meant to be used to aid in the learning process.

If you like videos like this consider contributing a dollar on Patreon.

Code And Transcript

Learn to Program 8

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

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

# ---------- READING & WRITING TEXT ----------

# The os module provides methods for file processing

importos

# We are able to store data for later use in files

# You can create or use an already created file with open

# If you use w (write) for mode then the file is

# overwritten.

# If you use a (append) you add to the end of the file

# Text is stored using unicode where numbers represent

# all possible characters

# We start the code with with which guarantees the file

# will be closed if the program crashes

withopen("mydata.txt",mode="w",encoding="utf-8")asmyFile:

# You can write to the file with write

# It doesn't add a newline

myFile.write("Some random text\nMore random text\nAnd some more")

# Open the file for reading

# You don't have to provide a mode because it is

# read by default

withopen("mydata.txt",encoding="utf-8")asmyFile:

# We can read data in a few ways

# 1. read() reads everything into 1 string

# 2. readline() reads everything including the first newline

# 3. readlines() returns a list of every line which includes

# each newline

# Use read() to get everything at once

print(myFile.read())

# Find out if the file is closed

print(myFile.closed)

# Get the file name

print(myFile.name)

# Get the access mode of the file

print(myFile.mode)

# Rename our file

os.rename("mydata.txt","mydata2.txt")

# Delete a file

# os.remove("mydata.dat")

# Create a directory

# os.mkdir("mydir")

# Change directories

# os.chdir("mydir")

# Display current directory

print("Current Directory :",os.getcwd())

# Remove a directory, but 1st move back 1 directory

# os.chdir("..")

# os.rmdir("mydir")

# ---------- PROBLEM : Fibonacci sequence ----------

# Previously we generated 1 number in the

# Fibonacci sequence. This time ask the user to define

# how many numbers they want and display them

# The formula for calculating the Fibonacci sequence is

# Fn = Fn-1 + Fn-2

# Where F0 = 0 and F1 = 1

# Sample Output

'''

How many Fibonacci values should be found : 30

1

1

2

3

5

All Done

'''

deffib(num):

ifnum==0:

return0

elifnum==1:

return1

else:

result=fib(num-1)+fib(num-2)

returnresult

numFibValues=int(input("How many Fibonacci values should be found : "))