properties = ["color", "font-size", "font-family", "width", "height"]
inPath = "style.css"
outPath = "output.txt"
#Open a file for reading
file = open(inPath, 'rU')
if file:
# read from the file
filecontents = file.read()
file.close()
else:
print "Error Opening File."
#Open a file for writing
file = open(outPath, 'wb')
if file:
for i in properties:
search = i
index = filecontents.find(search)
file.write(str(index), "\n")
file.close()
else:
print "Error Opening File."
seems to work, but:
- It only searches a keyword once?
- Its not writing to the output file.
function takes exactly 1 argument - I don’t want it to print the index actually, but the number of time the keyword appears.
Many thanks
First, you want
.count(search), not.find(search), if what you’re looking for is # of occurrences.Second,
.write()only takes a single parameter – if you want to write a newline, you need to concatenate it first, or call.write()twice.Third, doing
for i in properties: search = iis redundant; just use the name you want in your for loop.