adding list item to list object has no attribute '__getitem__' - python

I am very confused why the code below doesn't work under Python 2.7.6 in OS X.
Code should basically iterate through one list and add items to another list (I want to add conditions later)..
import os
home_dir = os.listdir("/Users/")
users_list = []
for user in home_dir:
users_list.append(user)
I get the error message below when running it:
Traceback (most recent call last): File "myfile.py", line x, in
<module>
users_list.append[suser] TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
/edit: weirdly enough when I do the same thing outside of the file in the python interpreter it seems to work ok?

You should really show the real code which produces the error.
Taken from the Traceback:
users_list.append[suser] - this is wrong
users_list.append(suser) - this is correct

Related

AttributeError: expected a callback function (and optionally one or two strings) as argument(s)

I'm new to Python and was trying to do some import and export scripting in Blender 2.43(which uses Python 2.4).
I had a simple script:
import Blender
import flt_import
import export_obj
import os
select_file=flt_import.select_file(r'C:\Code\Code\Testing4Flt\sg_navysoldier.flt')
Blender.Window.FileSelector(select_file, "Import OpenFlight", "*.flt")
out_file=export_obj.write_ui(r'C:\Code\Code\Testing4Flt\sg_navysoldier.obj')
Blender.Window.FileSelector(out_file,'Export Wavefront OBJ', sys.makename(ext='.obj'))
When run, I got the error in the title at line 8. I don't really understand what it means and I tried searching it but couldn't find that error exactly. Will appreciate any help.
Edit:
Here's the full traceback.
Traceback (most recent call last):
File "Testing4.py", line 8, in ?
AttributeError:
expected a callback function (and optionally one or two strings) as argument(s)

Cannot get any log information from pysimplesoap

I'm trying to use pysimplesoap (v.1.10) and getting what appears to be some kind of parsing error when executing a method request.
Stripped down version:
import pysimplesoap
soapURL = "https://site/path/to/soap"
namespace = "https://site/path/to/ns"
soapClient = pysimplesoap.client.SoapClient(location=soapURL + "?wsdl",namespace=namespace)
response = soapClient.getDocumentContent('1234567')
(python 2.7.8 btw)
Results in an error:
Traceback (most recent call last):
File ".\SOAPtest2.py", line 60, in <module>
response = soapClient.getDocumentContent('1234567')
File "build\bdist.win32\egg\pysimplesoap\client.py", line 139, in <lambda>
AttributeError: 'str' object has no attribute 'call'
However, the real question I have is that I am trying (unsuccessfully) to get logging working, but cannot see any output or determine/confirm what the XML structure it is sending/receiving. I might be able to diagnose the problem if I could see what it is receiving/trying to parse.
I have a gist of the code and the error I'm getting as well.
The odd part is that in my original script (before I stripped down to just some test code) I had a secondary logging instance and file handler and it worked just fine. So it seems specific to the pysimplesoap logging.
Any help would be greatly appreciated.
EDIT: Solution
Per KenK's recommendation, I modified my method call to be (documentId='1234567') and it worked. The script got past that error and I got a few log/debug lines in output. It seems that pysimplesoap simply has so few log/debug lines that none were reached prior to the error I was hitting.
Add the following code to your code:
import logging
logging.basicConfig(level=logging.DEBUG)
To fix the error you're getting, you need to specify an attribute name. Like this:
response = soapClient.getDocumentContent(name='1234567')
Replace name with whatever's defined for this function.

Python: AttributeError: 'str' object has no attribute 'decompressUnknownOriginalSize'

First off, I'm not a programmer by any means. I just try to follow instructions.
So, I'm trying to use a script to decode game files. The problem is that I'm getting an error.
I've tried the script on 2 different machines wheres both give the same error. At the same time, I have friends using the exact same script w/o getting this error. Does anyone have any clue what is wrong?
Traceback (most recent call last):
File "F:\Frostbite Decoding\Decoding Files\bf4dumper.py", line 258, in <module>
if "tocRoot" in locals(): dumpRoot(tocRoot)
File "F:\Frostbite Decoding\Decoding Files\bf4dumper.py", line 249, in dumpRoot
dump(fname,targetDirectory)
File "F:\Frostbite Decoding\Decoding Files\bf4dumper.py", line 198, in dump
LZ77.decompressUnknownOriginalSize(catEntry.path,catEntry.offset,catEntry.size,targetPath)
AttributeError: 'str' object has no attribute 'decompressUnknownOriginalSize'
If you need anymore information, please let me know.
Python is saying that it is receiving a string and that there is no method for decompressUnknownOriginalSize. This sounds like the script needs to get some other data type or argument type than string. Look for where "decompressUnknownOriginalSize" is called and see what data type is being passed to that argument.

Can't read from tempfile in Python

I am trying to use some temporary files in an little python script and running into problems while using the tempfile-module:
test-code
import tempfile
with tempfile.NamedTemporaryFile() as temp:
print temp.name
temp.write('Some data')
print temp.seek(0).read()
output
s$ python tmpFileTester.py
/var/folders/cp/8kmr4fhs4l94y8fdc6g_84lw0000gn/T/tmpcDgrzF
Traceback
(most recent call last): File "tmpFileTester.py", line 5, in
print temp.seek(0).read() AttributeError: 'NoneType' object has no attribute 'read'
--> This happens on my MacBook with MacOS 10.9 and Python 2.7.5, it also happens with "delete=False".
Any ideas? It's probably a noob-error, but I can't find the problem...
Whenever you are chaining methods in Python and you get this, you can bet that one of them returns None (even if implicitly by not returning at all). The solution is generally to split up the chain over multiple lines.
temp.seek(0)
print temp.read()
file.seek returns None.
You should separated following statement:
print temp.seek(0).read()
into two statements:
temp.seek(0)
print temp.read()

Calling a function from another file in Python

Yes, this question has been asked before. No, it did not answer my question satisfactorily.
So, I'm creating my Giraffe Program in Python first (don't ask) and I'm trying to get the user to name their giraffe.
My files are all in one package called code. In the file Main_Code, the function createGiraffe is called from code.Various_Functions. Here is my code.
import code
print("Welcome to The Animal Kingdom.")
userGiraffe = code.Various_Functions.createGiraffe()
And the code in code.Giraffes:
import code
def createGiraffe():
print("Name your giraffe.")
GiraffeName = input()
return GiraffeName
However, when I run Main_Code, it gives me this error:
Traceback (most recent call last):
File "C:\Users\Jonathan\Documents\Aptana Studio 3 Workspace\The Animal Kingdom\src\code\Main_Code.py", line 3, in <module>
userGiraffe = code.Giraffes.Giraffes.createGiraffe()
AttributeError: 'module' object has no attribute 'Giraffes'
How do I fix this? I believe that I've done everything by the book. It's all using the same package so I can call the function, I'm calling it from the right place, and the function has no syntax errors. Can anyone help with this?
Do
import code.Giraffes
before executing the offending line:
userGiraffe = code.Giraffes.Giraffes.createGiraffe()
When you call function like:
userGiraffe = code.Giraffes.Giraffes.createGiraffe()
it means you have a project in dir code with internal dir Giraffes and module Giraffes with function createGiraffe. Is this your exception?

Categories

Resources