Question: Python Assignment Reading and Writing Files Use comments to decribe your code Open a file. Type the following code: fhand = open(aFile.txt) print fhand What

Python Assignment Reading and Writing Files

Use comments to decribe your code

Open a file.

Type the following code:

fhand = open(aFile.txt)

print fhand

What do you see?

To see the contents of the file type:

for line in fhand:

print line

Counting tokens (in our case, words) in a file

words = 0

fhand = open('aFile2.txt')

for line in fhand:

print line # Use for development then remove

ln = line.split() # Transform a line of a file into a list

words += len(ln) # Returns the length of a list

print words # Use for development then remove

print Word count = , words # Print outside of the loop

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!