0
def fileCounter():
    infile = open('words.txt','r') # Open the source file
    outfile = open('File_Results.txt','w')
    data = infile.read()

    lineCount = len(data.split('\n'))
    wordCount = len(data.split())
    charCount = len(data)
    results = print(lineCount,wordCount,charCount)


    infile.close()
    outfile.write()
    outfile.close()

fileCounter()

I'm new to coding and this is my first time working with files. How do I write results in my outfield. I keep getting this error - TypeError: write() argument must be str, not None

1
  • Did any of these responses answer your question? If so, you should mark the best answer using the checkbox beside it. Commented May 8, 2017 at 3:26

5 Answers 5

1

You need to write something. Something goes between the parentheses for outfile.write(). My guess is that you want something like this:

outfile.write("{} {} {}".format(lineCount, wordCount, charCount))

Your result = print(...) doesn't save anything. It prints to your console.

Another approach would be redirecting your prints to your file:

from contextlib import redirect_stdout

def fileCounter():
    with (open('words.txt','r') as infile, open('File_Results.txt','w') as outfile):
        data = infile.read()

        lineCount = len(data.split('\n'))
        wordCount = len(data.split())
        charCount = len(data)

        with redirect_stdout(outfile):
            print(lineCount,wordCount,charCount)

fileCounter()

Note that I also used context managers to automatically handle opening and closing files. This approach is safer because it'll close the files (and stop redirecting STDOUT) even if an exception occurs.

Sign up to request clarification or add additional context in comments.

Comments

1

There is no argument to your outfile.write() function. There needs to be some content to be written to the file, that needs to be passed as parameter to the function.

For Example:

 # To write 'I am new to Python'
 outfile.write('I am new to Python')

Comments

0

the argument to the write function must be a string.

if this line

results = print(lineCount,wordCount,charCount)

prints the stuff you want to have in the output file, you might do something like

results = "%s, %s, %s" % (lineCount,wordCount,charCount)
outfile.write(results)
outfile.close()

Comments

0

There are no arguments in outfile.write() so it writes nothing.

I assume you want to write the data of infile in outfile, so you do the following:

outfile.write(lineCount)
outfile.write(wordCount)
outfile.write(charCount)

1 Comment

May be helpful to include separators, since this code doesn't put spaces or newlines between the output.
0

In outfile.write() you wish to include whatever you're writing to file. In this case, you could do something like:

#put results into a list
results = [lineCount, wordCount, charCount]

#print results
print(results)

#write results to a file
outfile.write(",".join(results))

Two things in your code that are interesting. First, as far as I'm aware, print returns None so results in your current code is None. Second, in the corrected rendition, results is a list but in order to write it to file you need to convert it to a string. We do that by joining the elements in the list, in this case with a comma.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.