os.path.basename работает с URL, почему?

Насколько я знаю, что общее решение должно добавить ? к ссылке src сценария.

, Например:


я предполагаю в этой точке, что нет лучшего пути, чем находить-замена для постепенного увеличения этих "номеров версий" во всех тегах script?

у Вас могла бы быть система управления версиями, делают это для Вас? Большинство систем управления версиями имеет способ автоматически ввести число пересмотра на регистрации, например.

Это выглядело бы примерно так:


, Конечно, всегда существуют лучшие решения как этот .

12
задан Grace Note 12 May 2010 в 18:46
поделиться

4 ответа

In practice many functions of os.path are just string manipulation functions (which just happen to be especially handy for path manipulation) -- and since that's innocuous and occasionally handy, while formally speaking "incorrect", I doubt this will change anytime soon -- for more details, use the following simple one-liner at a shell/command prompt:

$ python -c"import sys; import StringIO; x = StringIO.StringIO(); sys.stdout = x; import this; sys.stdout = sys.__stdout__; print x.getvalue().splitlines()[10][9:]"

Or, for Python 3:

$ python -c"import sys; import io; x = io.StringIO(); sys.stdout = x; import this; sys.stdout = sys.__stdout__; print(x.getvalue().splitlines()[10][9:])"
19
ответ дан 2 December 2019 в 05:41
поделиться

Почему? Потому что это полезно для синтаксического анализа URL-адресов, а также локальных путей к файлам. Почему бы и нет?

0
ответ дан 2 December 2019 в 05:41
поделиться

Use the source Luke:


def basename(p):
    """Returns the final component of a pathname"""
    i = p.rfind('/') + 1
    return p[i:]

Edit (response to clarification):

It works for URLs by accident, that's it. Because of that, exploiting its behaviour could be considered code smell by some.

Trying to "fix" it (check if passed path is not url) is also surprisingly difficult

www.google.com/test.php
me@other.place.com/12
./src/bin/doc/goto.c

are at the same time correct pathnames and URLs (relative), so is the http:/hello.txt (one /, and only on linux, and it's kinda stupid :)). You could "fix" it for absolute urls but relative ones will still work. Handling one special case in differently is a big no no in the python world.

To sum it up: import this

2
ответ дан 2 December 2019 в 05:41
поделиться

В Windows посмотрите исходный код: C: \ Python25 \ Lib \ ntpath.py

def basename(p):
    """Returns the final component of a pathname"""
    return split(p)[1]

os.path.split (в том же файле) просто разделил "\" (и sth. Else)

3
ответ дан 2 December 2019 в 05:41
поделиться
Другие вопросы по тегам:

Похожие вопросы: