HOME - Recent Changes - Search:

Academic Work


Personal

* pot de départ


dblp


(:twitter:)

-----

[ edit | logout ]
[ help | sandbox | passwd ]

Splitting and joining a path/URL

#############################

Treating local files

Splitting:

import os

file = '/home/jabba/python/filepath/hello.py'

(dirName, fileName) = os.path.split(file)
print dirName    # /home/jabba/python/filepath
print fileName   # hello.py

(fileBaseName, fileExt) = os.path.splitext(fileName)
print fileBaseName   # hello
print fileExt        # .py

Joining:

import os

print os.path.join('/trash/movies', 'film.avi')    # (no trailing slash)   /trash/movies/film.avi
print os.path.join('/trash/movies/', 'film.avi')   # (with trailing slash) /trash/movies/film.avi

Notice that you don't have to bother about the trailing slash.

Treating URLs

Splitting:

import os
from urlparse import urlparse

url = 'http://japancsaj.com/pic/rju_ri/rju_ri_9.jpg'
fileName = os.path.split(urlparse(url)[2])[1]
print fileName   # rju_ri_9.jpg

See also http://docs.python.org/library/urlparse.html#urlparse.urlparse .

Joining:

import urlparse

base='http://example.com/'
file='img.jpg'
print urlparse.urljoin(base, file)   # http://example.com/img.jpg

Don't bother about the trailing slash.

Cloud City


anime | bash | blogs | bsd | c/c++ | c64 | calc | comics | convert | cube | del.icio.us | digg | east | eBooks | egeszseg | elite | firefox | flash | fun | games | gimp | google | groovy | hardware | hit&run | howto | java | javascript | knife | lang | latex | liferay | linux | lovecraft | magyar | maths | movies | music | p2p | perl | pdf | photoshop | php | pmwiki | prog | python | radio | recept | rts | scala | scene | sci-fi | scripting | security | shell | space | súlyos | telephone | torrente | translate | ubuntu | vim | wallpapers | webutils | wikis | windows


Blogs and Dev.

* Ubuntu Incident
* Python Adventures
* me @ GitHub


Places

Debrecen | France | Hungary | Montreal | Nancy


Notes

full circle | km


Hobby Projects

* Jabba's Codes
* PmWiki
* Firefox
* PHP
* JavaScript
* Scriptorium
* Tutorials
* me @ GitHub


Quick Links


[ edit ]

View - Edit - History - Attach - Print *** Report - Recent Changes - Search
Page last modified on 2009 November 22, 02:48