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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started