Javascript to Python
Sean 'Shaleh' Perry
shalehperry at attbi.com
Fri Mar 1 12:26:44 EST 2002
On 01-Mar-2002 Sam Collett wrote:
> Does anyone know of any good sites on Javascript > Python examples?
> I have a function in javascript that returns the file extension:
>
> function FileExtension(sFilePath)
> {
> return Right(sFilePath,sFilePath.length-sFilePath.lastIndexOf("."))
> }
>
> so FileExtension("/path/to/file.doc") would return .doc
> I would also want to return the filename (file) and the path
> (/path/to/). How would I go about that?
>
import os, os.path
>>> filename = '/path/to/foo.doc'
>>> os.path.splitext(filename)
('/path/to/foo', '.doc')
>>> os.path.splitext(os.path.basename(filename))
('foo', '.doc')
>>> os.path.basename(filename)
'foo.doc'
>>> os.path.splitext(filename)
('/path/to/foo', '.doc')
>>> os.path.splitext(os.path.basename(filename))
('foo', '.doc')
os.path has a bunch of nice path related tools like abspath() and join().
Check out the python docs.
> What equivalents to the functions Right and Left (right side of
> string, left side of string)
>
sorry, not quite sure what the Right side of a string is (-:
> Also is there an easier way to do number incrementing (just me being
> lazy):
> while r<5
> print r
> r=r+1
>
> I have tried r++ to add 1 each time, and r-- to take away one, but
> this does not work, so is the only way r=r+1 or r=r-1 ?
>
in python 1.5.2 and earlier the only way was r = r + 1. In the 2.x series the
+= operator was added (I think this works in javascript too, it works in most C
like languages) r += 1. There is equivalents for the other standard math ops.
More information about the Python-list
mailing list