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.
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.
Related
I want to add functions to python's built in module. As a example, there is no find function in list. I want to add it. There is some other functions I want to add. Can somebody please tell me how can I do that? Is their any way I can do that?
Adding functions to the builtins module is simple:
import builtins
builtins.find = my_find_function
But the built in classes are usually immutable, so they cannot be changed.
>>> list.hello = "hello"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'list'
I know we can import modules and just embed Python code in C++ and evaluate it. But how can I use built-in functions like print or open? These functions off course aren't module. Evaluating embedded open statement just gives me the following error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'open' is not defined
Stuck. Please help me.
Try importing the builtins and io module and if you want any other function just call the __module__ attribute to find about which module to import
>>> print.__module__
'builtins'
>>> open.__module__
'io'
This seems like a simple issue but I'm having a very difficult time understanding why I am getting the following error:
Traceback (most recent call last):
File "....py", line 46, in update
self.Grob3Text.SetLabel('Grob 3: ' + str(Grob3))
AttributeError: 'Frame' object has no attribute 'Grob3Text'
When running the following script:
Please see -
.
I understand the message is telling me that the static text variable 'Grob3Text' is not defined in the Frame, but it appears to me that the definition for update event is under the same class. All I'm trying to do is execute the faultreport function and update the static text. I have got this working in another script but for whatever reason I am overlooking something here.
You create a local variable Grob3Text in __init__, but you don't actually store it as an instance attribute on the class instance. To do that, you could simply do:
self.Grob3Text = Grob3Text
in __init__ after you create Grob3Text.
I've got file named recommend.py. It has a dict data named critics.
When I try to reload it in the interpreter it gives the following error:
>>> from recommend import critics
>>> reload(recommend.py)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'recommend' is not defined
>>>
I'm new to python. Please help me.
recommend.py is parsed as recommend . py which means that python looks for an object bound to the name recommend and then tries to get the py attribute from it. That doesn't work because you don't have an object named recommend in the current namespace and because even if you did have an object bound to that name, it probably wouldn't have an attribute py.
Of course, you'll need to give reload an actual module object. Something more like:
import recommend
reload(recommend)
reload() takes a module object, not a filename:
import recommend
reload(recommend)
I have these lines in Python:
page = lxml.html.parse(URL).getroot()
table = only(page.cssselect('table[width=510]'))
What is the only method doing? I can't find it in the Python docs (though that might just be because it's very hard to search for!)
thanks.
There is no only built-in function, as you'll see if you type help(only) into your Python interpreter.
It must be pulled into the namespace with a from <module> import <only|*> instruction in that module. When you find this, you could try importing the module in your Python interpreter and using the help function again to find out what it does.
You can try to figure out which module only is defined in by examining the import statements in your file. Then look up only in the docs for that module. Or just put a print only.__module__ in your code and that might print out the module.
AFAIK, it isn't standard. But I'd guess that it fetches the only element from its input, or raises an exception if the input doesn't have exactly one element. E.g.:
>>> def only(input):
... [result] = input
... return result
...
>>> only([12])
12
>>> only([])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in only
ValueError: need more than 0 values to unpack
>>> only([23, 34])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in only
ValueError: too many values to unpack
>>>
only is not a built-in function. Is it defined or imported anywhere in the .py file that you have? If not, look for from somemodule import *, and then look in each occurrence of somemodule.