Why is the next code not working? I cant find why.
import mimetypes
import glob, urllib
for file in glob.glob("C:\\Users\\joey\\Desktop\\school\\ICOMMH"):
url = urllib.pathname2url(file)
print(file, mimetypes.guess_type(url))
The error message I get is:
AttributeError: 'module' object has no attribute 'pathname2url'
I am trying display all file typs of a directory. Is this a good way? Or is there a better way. I dont want to use the module magic.
This function's location has changed in Python 3. It is now urllib.request.pathname2url.
Related
This is my first ever Python script, so I assume I'm doing something wrong. But I can't find a clue in any of the tutorials or examples. The following code (so to speak):
import urllib
urllib.retrieve("http://zlib.net/zlib-1.2.8.tar.gz")
throws an error
AttributeError: 'module' object has no attribute 'retrieve'
How do I fix it? This is Python 3.3.
[The question was solved in the comments, hence adding it as an answer now.]
The below code works. (Source : this answer)
import urllib.request
# Download the file from `url` and save it locally under `file_name`:
urllib.request.urlretrieve(url, file_name)
Note the import statement.
You need to do import urllib.request instead of import urllib.
As the error says, urllib does not have a retrieve function.
In Python 2, the module did have a urlretrieve function. In Python 3, that function has been moved to urllib.request.urlretrieve.
You can find all this in the documentation: https://docs.python.org/3/library/urllib.html
Maybe I am completely missing something here but when I run this code form the shell it works:
import nltk
tokens = nltk.word_tokenize("foo bar")
and returns:
['foo','bar']
But when I but this into a file and execute it with python -u "path/to/file/myfile.py" it returns
AttributeError: 'module' object has no attribute 'word_tokenize'
I've tried reinstalling and every thing i can think of. Let me know if you need any more information.
Thanks in Advance!
You have more than likely called your file nltk.py so python is trying to import from that as opposed to the actual nltk module. Just rename your .pyfile.
I was trying to create a class in python with 'RawIOBase' as given below.
try:
import io
except ImportError:
class Serial(PosixSerial, FileLike):
pass
else:
class Serial(PosixSerial, io.RawIOBase):
pass
I was trying to run this using Python 2.6, but it is displaying the error:
AttributeError: 'module' object has no attribute 'RawIOBase'
I read that RawIOBase is supported from Python 2.6 onwards.
Make sure you do not have another file named io.py. If so, it could mask the io module in the standard library. You can check which file is getting loaded as the io module by printing print(io). It should return something like <module 'io' from '/usr/lib/python2.6/io.pyc'>.
If there is such a module or package masking the standard lib module, the solution is to rename the non-standard io module or package.
Something strange is happening. I am getting an error when running this code in Sublime Text 2 while the code is valid elsewhere.
import copy
s = 'string'
cs = copy.copy(s)
print s == cs
I got the TypeError: 'module' object is not callable
Also, copy.deepcopy() throws an error AttributeError: 'module' object has no attribute 'deepcopy' while running inside of ST2.
I am aware this is the ST2 specific problem, but maybe some of you know whether this can be solved?
It looks like you've masked the built-in copy module my adding your own copy module somewhere in the module search path used by sublimetext2.
To fix that rename your copy.py file to something else and also delete the copy.pyc file.
The location of the file can be found using __file__ attribute of the module object.
import copy
print copy.__file__
In future please don't name your modules or packages same as python built-in modules, otherwise you'll face same issues.
I first tried with the interpreter to produce uuid's with python's uuid module. I did the following:
>>>import uuid
>>>uuid.uuid1()
UUID('d8904cf8-48ea-11e0-ac43-109add570b60')
So far so good. I create a simple little function to produce the uuid's.
import uuid
def get_guid():
return uuid.uuid1()
if __name__ == '__main__':
print get_guid()
and I get the following error:
AttributeError: 'module' object has no attribute 'uuid1'
Ok...hmm...go back to the interpreter and now it too is broken. I get the same error running the same code I used to test this. I am baffled. What makes uuid break like this? And what is wrong with my code?
I am using python 2.6
Your test file name is most likely named uuid.py
When you went back to the interpreter, you launched the interpreter from the same directory, which by default, will first look for the module name to import in your current working directory.
Just change your test file name to something else, i.e. uuid_test_snippet.py