Hello.

I need some help. I have a function my_function(a, b, c, d) which takes string parameters and output them in different combinations. It works well in IDLE, but I would like to output the result into a file. Such code

file = open("out_file.txt", 'w')
file.write(my_function(a, b, c, d))

generates a Type Error (must be string). What could you advice?

Thank you.

You'll have to post the code for my_function(a, b, c, d) or whatever it is called. This statement
file.write(my_function(a, b, c, d))
would write the return value. Does the function return a value, and if so, what does it return?

You'll have to post the code for my_function(a, b, c, d) or whatever it is called. This statement
file.write(my_function(a, b, c, d))
would write the return value. Does the function return a value, and if so, what does it return?

The function is as follows:
camp is a string, root and filler are lists of strings. The output is a collection of strings.

def keyword_output(camp, root, filler):

    k = 0
    while k < len(filler):
        for i in range(0, len(root)):
            print camp + '\t', root[i] + '\t', filler[k], root[i]
        k = k + 1

Actually the function is larger, but it repeats the same things few times more. There are just more data to manipulate.

The problem is solved. The thing was that I tried to pass a few arguments to the write method (I treated it the same way as print).

The problem is solved. The thing was that I tried to pass a few arguments to the write method (I treated it the same way as print).

Strange it worked! The function have no return value. What are output to the file? Can you post new code?

Strange it worked! The function have no return value. What are output to the file? Can you post new code?

I tried to do it with "brute force". I've forgotten about the return value and failed. Then I just added writing to the file as a part of the function, not of the main program:

ms = " " #A provisory new string
def keyword_output(camp, root, filler):
    k = 0
    while k < len(filler):
        for i in range(0, len(root)):
            ms = ms + camp +'\t'+root[i]+'\t'+ filler[k] + ' '+ root[i]+'\n'
            #This is instead of print command which I used for IDLE
    k = k + 1

    #After the cycle, this new string is output into a file.
    #Program which calls this function generates the file. 
    fout = open("text.txt", "w")
    fout.write(ms)      
    fout.close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.