Python 8 Flashcards

1
Q

How to open a file?

A

file_object = open(file_name, access_mode)
-file_name and access_mode are strng and need quotes around them

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

If you wanted to open a text file with a name hello what to do?

A

file_obkect = open(‘hello.txt’, “r”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are all the access modes and what do they do?

A

-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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are all the file object attributes?

A

-file.closed: True if file is closed, False eitherwise
-file.mode: Returns access mode for file
-file.name: Returns name of file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A text file called foo on w access mode, print out the name, if file is closed and the access code.

A

fo = open(‘foo.txt’,’w’
print (‘Name of the file: ‘, fo.name)
print(‘Closed for not: ‘, fo.closed)
print (‘Opening mode: ‘, fo.mode)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to print a note.txt in python?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to read all the lines in a file in a list

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What would it look like if you use readline in IDLE?

A

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’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to write on a file that has 3 lines?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to append a file

A

my_file = open(“note.txt”,”a”)
my_file.write(“\nThis is the fourth line.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to use with as a keyword?

A

with open(“note.txt”,”r”) as my_file
line = my_file.readline()

-You wont need to use my_file.close()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to spilt this string into each item being one name.
Ben, Sam, James, Alex, Tom, Brad

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to strip a string?
line = ‘this is the first line.\n’

A

-Use strip() and have the part you want removed inside the brackets
line.strip(‘/n’)
This is the first line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Suppose you want to read a csv file and calculate the total of each row how to do it?

A

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)