I decided that I'll learn python tonight :) I know C pretty well (wrote an OS in it) so I'm not a noob in programming so everything in python seems pretty easy, but I don't know how to solve this problem : let's say I have this address:
http://example.com/random/folder/path.html Now how can I create two strings from this, one containing the "base" name of the server, so in this example it would be http://example.com/ and another containing the thing without the last filename, so in this example it would be http://example.com/random/folder/ . Also I of course know the possibility to just find the 3rd and last slash respectively but maybe you know a better way :] Also it would be cool to have the trailing slash in both cases but I don't care since it can be added easily. So anyone has a good, fast, effective solution for this? Or is there only "my" solution, finding the slashes?
Thanks!
Thank you very much to the other answerers here, who pointed me in the right direction via the answers they have given!
It seems like the posixpath module mentioned by
sykora
's answer is not available in my Python setup (python 2.7.3).As per this article it seems that the "proper" way to do this would be using...
urlparse.urlparse
andurlparse.urlunparse
can be used to detach and reattach the base of the URLos.path
can be used to manipulate the pathurllib.url2pathname
andurllib.pathname2url
(to make path name manipulation portable, so it can work on Windows and the like)So for example (not including reattaching the base URL)...
You can use python's library furl:
To access word after first "/", use:
In Python a lot of operations are done using lists. The urlparse module mentioned by Sebasian Dietz may well solve your specific problem, but if you're generally interested in Pythonic ways to find slashes in strings, for example, try something like this:
The output of this program is this:
The interesting bits are
split
,join
, the slice notation array[A:B] (including negatives for offsets-from-the-end) and, as a bonus, the%
operator on strings to give printf-style formatting.The urlparse module in python 2.x (or urllib.parse in python 3.x) would be the way to do it.
If you wanted to do more work on the path of the file under the url, you can use the posixpath module :
After that, you can use posixpath.join to glue the parts together.
EDIT: I totally forgot that windows users will choke on the path separator in os.path. I read the posixpath module docs, and it has a special reference to URL manipulation, so all's good.
I have no experience with Python, but I found the urlparse module, which should do the job.
If this is the extent of your URL parsing, Python's inbuilt rpartition will do the job:
From Pydoc, str.rpartition:
Splits the string at the last occurrence of sep, and returns a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself
What this means is that rpartition does the searching for you, and splits the string at the last (right most) occurrence of the character you specify (in this case / ). It returns a tuple containing: