“Python os.path.join” Ответ

Python присоединяется к путям

import os

# Join paths using OS import. Takes any amount of arguments
path = os.path.join('/var/www/public_html', './app/', ".\\my_file.json")

print(path) # /var/www/public_html/app/myfile.json
Allen

ОС ПАТКА SPLITEXT

import os
basename, ext = os.path.splitext('inputfile.txt')
bougui

Python os.path.join

>>> p = os.path.join(os.getcwd(), 'foo.txt')
>>> p
'/Users/csaftoiu/tmp/foo.txt'
>>> os.path.dirname(p)
'/Users/csaftoiu/tmp'
>>> os.path.basename(p)
'foo.txt'
>>> os.path.split(os.getcwd())
('/Users/csaftoiu/tmp', 'foo.txt')
>>> os.path.splitext(os.path.basename(p))
('foo', '.txt')
David Cao

Присоединяйтесь к пути Pathlib

# The correct operator to extend a pathlib object is /
from pathlib import Path

dir_path = Path('directory')
subdir_path = dir_path / "subdir_path"
OsaSadogh

OS.Path.sep.join

1
2
>>> os.path.join("foobar", "/foo/baz/", "whatever")
'/foo/baz/whatever'
Noman Nosher

OS.Path.sep.join

1
2
3
4
>>> type(os.sep)
<class 'str'>
>>> os.sep.join(["foobar", "/foo/baz/", "whatever"])
'foobar//foo/baz//whatever'
Noman Nosher

Ответы похожие на “Python os.path.join”

Вопросы похожие на “Python os.path.join”

Больше похожих ответов на “Python os.path.join” по Python

Смотреть популярные ответы по языку

Смотреть другие языки программирования