1/40
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
open function
open(file, mode)
close function
close()
read method
file object method that reads entire file contents into memory, only works if file has been opened for reading, contents returned as a string
read position
marks the location of the next item to be read from a file
reading data from a file
f = open(“demofile.txt“, “r“)
print(f.read())
returns the 5 first characters of the file
f = open(“demofile.txt“, “r“)
print(f.read(5))
seek
f=open(“test.txt“, “r“)
f.seek(3)
f.read()
starts reading from the 3rd character
readline method
file object method that reads a line from the file, line returned as a string including ‘\n’
read one line of the file
f = open(“demofile.txt“, “r“)
print(f.readline())
read two lines of the file
f = open(“demofile.txt“, “r“)
print(f.readline())
print(f.readline())
readlines() method
returns a list of strings, where the first element is the contents of the first line, the second element is the contents of the second line, and so on
readlines syntax
f = open(“demofile.txt“, “r“)
print(f.readlines())
open a file in write mode and close manually
outfile = open(“text.txt“, “w“)
outfile.write(“Test“)
outfile.close()
str function
converts value to string
for loop with file syntax
for line in file_object:
statements
opens a text file in write mode and automatically closes it
with open(“text.txt“, “w“) as outfile:
outfile.write(“Test“)
code that opens a text file in read mode and automatically closes it
with open(“test.txt“, “r“) as infile:
print(infile.readline())
write method of a file object
write(str)
write one line to a text file
with open(“members.txt“, “w“) as file:
file.write(“John Cleese \n“)
append one line to a text file
with open(“members.txt“, “a“) as file:
file.write(“Eric Idle \n“)
use a loop to read each line of the file
with open(“members.txt“) as file:
for line in file:
print(line, end=””)
print()
read the entire file as a string
with open(“members.txt“) as file:
contents = file.read()
print(contents)
read entire file as a list
with open(“members.txt“) as file:
members = file.readlines()
print(members[0], end=””)
print(members[1])
read each line of the file
with open(“members.txt“) as file:
member1=file.readline()
print(member1, end=””)
member2=file.readline()
print(member2)
write the items in a list to a file
members = [“John Cleese“, “Eric Idle“]
with open(“members.txt“, “w“) as file:
for m in members:
file.write(m, “\n“)
read line in a file into a list
members = []
with open(“members.txt“) as file:
for line in file:
line = line.replace(“\n“, ““)
members.append(line)
print(members)
write the items in a list to a file
years = [1975, 1979, 1983]
with open(“years.txt“, “w“) as years_file:
for year in years:
years_file.write(str(year) + “\n“)
read items in a list from a file
years = []
with open(“years.txt”) as file:
for line in file:
line = line.replace(“\n“, ““)
years.append(int(line))
print(years)
writer function for csv
writer(file)
writerows method for csv
writerows(rows)
write the list to a csv file
with open(“movies.csv“, “w“, newline=””) as file:
writer = csv.writer(file)
writer.writerows(movies)
reader function for csv
reader(file)
read data from csv file
with open(“movies.csv“, newline=””) as file:
reader = csv.reader(file)
for row in reader:
print(row[0] + “ (“ + str(row[1]) + “)“)
pickle module
binary protocols for serializing and de-serializing a python object, two main methods of dump and load
dump method
dumps an object to a file object, dump(object, bfile)
load method
loads an object from a file object, load(bfile)
write an object to a binary file
with open(“movies.bin“, “wb“) as file:
pickle.dump(movies, file)
read an object from a binary file
with open(“movies.bin“, “rb“) as file:
movie_list = pickle.load(file)
print(movie_list)
os module
provides an interface to operating system function calls
os module syntax
import os
myfile=open(“myfile.txt“, “r“)
file_info = os.stat(“mfile.txt“)
os.remove(“myfile.txt“)
os.walk()
“walks“ a directory tree, visiting each subdirectory in the specified path