Extra: Python Coding Flashcards
What are the different file access functions in python?
Read -> f = open(“FileName.txt”, “r”)
Write -> f = open(“FileName.txt”, “w”)
Append -> f = open(“FileName.txt”, “a”)
Create -> f = open(“FileName.txt”, “x”)
What is the code to read a file? (or by first line)
f = open(“FileName.txt”, “r”)
text = f.read() // text = f.readline()
f.close()
How do you read a file line by line?
f = open(“FileName.txt”, “r”)
for x in f:
print(x)
How do you write onto a file and what does it do?
f = open(“FileText.txt”, “w”)
f.write(“newText”)
f.close()
This deletes the current contents stored on the file and give it new contents
How do you append a file and what does it do?
f = open(“FileText.txt”, “a”)
f.write(“newText”)
f.close()
Give the file new contents that will appear at the end of the file. If the file doesn’t exist, it will create the file
How do you run a statement for each content in a 2D array?
array = [“John”, “IceCream”, “Burgers”], [“Sahra”, “Burgers”, “Chilli”], [“Michal”, “Chips”, “Curry”], [“Janet”, “Popcorn”, “Doritos”]
for row in array:
for item in row:
statement(s)
How could you find a particular item in a 2D array?
array = [“John”, “IceCream”, “Burgers”], [“Sahra”, “Burgers”, “Chilli”], [“Michal”, “Chips”, “Curry”], [“Janet”, “Popcorn”, “Doritos”]
print(array[1][2])
“Chilli”