landucci
landucci

Reputation: 11

Python Writing data to output File

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

Upvotes: 0

Views: 29909

Answers (5)

Sagar Thatte
Sagar Thatte

Reputation: 75

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')

Upvotes: 1

Gene Burinsky
Gene Burinsky

Reputation: 10203

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.

Upvotes: 0

Exercise To The Reader
Exercise To The Reader

Reputation: 617

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)

Upvotes: 0

Jon Kiparsky
Jon Kiparsky

Reputation: 7743

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()

Upvotes: 0

Arya McCarthy
Arya McCarthy

Reputation: 8829

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.

Upvotes: 1

Related Questions