Python 8 Flashcards
How to open a file?
file_object = open(file_name, access_mode)
-file_name and access_mode are strng and need quotes around them
If you wanted to open a text file with a name hello what to do?
file_obkect = open(‘hello.txt’, “r”
What are all the access modes and what do they do?
-r: Open file for reading
-r+:Open file fo reading and writng
-w: Open file for writing, overwrites if file exists
-w+:Opens file for writing and reading
-a: Opens file for appending. Makes new file if file doesnt exists
-a+:Opens file for reading and appending
What are all the file object attributes?
-file.closed: True if file is closed, False eitherwise
-file.mode: Returns access mode for file
-file.name: Returns name of file
A text file called foo on w access mode, print out the name, if file is closed and the access code.
fo = open(‘foo.txt’,’w’
print (‘Name of the file: ‘, fo.name)
print(‘Closed for not: ‘, fo.closed)
print (‘Opening mode: ‘, fo.mode)
How to print a note.txt in python?
my_file1 = open(‘note.txt”,”r”)
text = my_file1.read()
my_file1.read()
print(text)
-We could have assigned anyname to the next
-If there is a number in the text=my_file1.read() this means the print would be limited to the first 8 characters including spaces
How to read all the lines in a file in a list
my_file1 = open(“note.txt”,”r”)
text = my_file1.read(lines)
my_file1.read()
print(text)
Output:
(‘Whatever was on text.\n’, ‘If remaining text was in a newline.\n, ‘This is the last line’)
-Last line in text doesnt end in \n
What would it look like if you use readline in IDLE?
my_file = open(“note.txt”,”r”)
line = my_file.readline()
line
‘First line will show. \n’
line =my_file.readline()
line
‘Second line will show. \n’
line = my_file.readline()
line
‘And third line will show. \n’
What to do if you want the output to look like this?
first line = First line will show.
second line = Second line will show.
third line = third line will show
my_file = open(“note.txt”,”r”)
first_line = my_file.readline()
second_line=my_file.readline()
third_line=my+file.readline()
my_file.close()
print (“first line = ‘, first_line)
print (‘second line = ‘, second_line)
print(“third line = ‘,third_line)
How to write on a file that has 3 lines?
my_file = open(“note.txt”,”w”)
my_file.write(“Whatever you want.\nWhatever you say.\n”+”Whatever you need”
my_file.close()
Important
-Dont have space between \n and next line and last line add the +
How to append a file
my_file = open(“note.txt”,”a”)
my_file.write(“\nThis is the fourth line.)
How to use with as a keyword?
with open(“note.txt”,”r”) as my_file
line = my_file.readline()
-You wont need to use my_file.close()
How to spilt this string into each item being one name.
Ben, Sam, James, Alex, Tom, Brad
names = “Ben, Sam, James, Alex, Tom, Brad”
names.spilt(‘,’)
[‘Ben’,’Sam’,’James’,’Alex’,’Tom’,’Brad’]
Important notes
-If its spilt () without anything inside it will spilt the string whenever there is white space
How to strip a string?
line = ‘this is the first line.\n’
-Use strip() and have the part you want removed inside the brackets
line.strip(‘/n’)
This is the first line
Suppose you want to read a csv file and calculate the total of each row how to do it?
my_file = open(“data.csv”,”r”)
data = my_file.readlines()
for i in range (0, len(data)):
print (‘row’+str(i)+’: ‘ ,data[i])
for i in range (0, len(data)):
x = data[i].strip(‘\n’)
xa = x.spilt(‘, ‘)
xaa = float(xa[0]) + float(xa[1])
print (‘total of row’ +stri (i) + ‘: “,xaa)