Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions