python 3.4.2 urlib no attribute 'pathname2url'
Asked Answered
D

1

9

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.

Droll answered 4/6, 2015 at 21:47 Comment(0)
T
16

This function's location has changed in Python 3. It is now urllib.request.pathname2url.

Tussle answered 4/6, 2015 at 21:50 Comment(2)
Hey Thanks for helping me. Maby a stupid question but I replaced 'url = urllib.pathname2url(file)' to: url = urllib.request.pathname2url(file) But It stil display a error message. [AttributeError: 'module' object has no attribute 'request']Droll
@Droll you need to either import urllib.request, in which case the code in your comment will work as-is, or do something like from urllib.request import pathname2url, in which case you should change your code to url = pathname2url(file). Also, while using file as a variable name is technically OK, file is a built-in function in Python 2, so if you'll ever be working in that version of the language you'll have to be careful about masking it.Tussle

© 2022 - 2024 — McMap. All rights reserved.