“Python os.path.join” Kode Jawaban

Python bergabung dengan jalur

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

OS Path 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

Bergabunglah di Pathlib Path

# 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

Jawaban yang mirip dengan “Python os.path.join”

Pertanyaan yang mirip dengan “Python os.path.join”

Lebih banyak jawaban terkait untuk “Python os.path.join” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya