Kludge
Kludge

Reputation: 29

python read txt files into single file

Looking to read a directory of sql queries in separate files into a single file

I'm a beginner in python but I figured this task would be an easy enough start.

EQ = open("EnmaxQueries.SQL","a+")
>>> for file in dir:
...     with open(file,"r") as reader:
...             EQ.write(reader.read())
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'builtin_function_or_method' object is not iterable

What does the error refer to? Usually it appear to referring to a function rather than its output but I thought my function calls all used ().

Should I be approching this in another way? Have I made an error in scope of the loop or faile dto execute the multi line block properly or something?

EDIT:dir was defined by os.listdir and this refers to the method listdir and not it's output as is typical for this error.

Upvotes: 0

Views: 58

Answers (1)

sleeplessnerd
sleeplessnerd

Reputation: 22761

dir was not defined by you and is defaulting to the python dir() function that lists members of python objects.

Maybe you want to have a look at os.listdir.

Upvotes: 2

Related Questions