Hướng dẫn relative path python - Python đường dẫn tương đối

981

Nội dung chính

  • Làm thế nào để bạn tìm thấy con đường tuyệt đối từ một con đường tương đối?
  • Làm thế nào để bạn chuyển đổi đường dẫn tương đối sang đường dẫn tuyệt đối trong Python?
  • Làm thế nào để bạn tìm thấy con đường tuyệt đối của một con đường trong Python?
  • Làm thế nào để bạn truy cập vào đường dẫn tương đối trong Python?

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.Learn more.
Learn more.

Đưa ra một đường dẫn như

>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
9, làm thế nào để tôi tìm thấy đường dẫn tuyệt đối của tệp trong Python? Ví dụ. Trên Windows, tôi có thể kết thúc với:
"C:/example/cwd/mydir/myfile.txt"

Hướng dẫn relative path python - Python đường dẫn tương đối

hỏi ngày 9 tháng 9 năm 2008 lúc 10:19Sep 9, 2008 at 10:19Sep 9, 2008 at 10:19

0

>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'

Cũng hoạt động nếu nó đã là một con đường tuyệt đối:

>>> import os
>>> os.path.abspath("C:/example/cwd/mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'

Héo úa

2.5831 Huy hiệu vàng19 Huy hiệu bạc28 Huy hiệu đồng1 gold badge19 silver badges28 bronze badges1 gold badge19 silver badges28 bronze badges

Đã trả lời ngày 9 tháng 9 năm 2008 lúc 10:21Sep 9, 2008 at 10:21Sep 9, 2008 at 10:21

Sherbangsherbangsherbangsherbang

Huy hiệu vàng 15.2K11 gold badge22 silver badges16 bronze badges1 gold badge22 silver badges16 bronze badges

8

Bạn có thể sử dụng Thư viện Python 3.4 mới

>>> import os
>>> os.path.abspath("C:/example/cwd/mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
0. .

Để có được một đường dẫn tuyệt đối trong Windows:

>>> from pathlib import Path
>>> p = Path("pythonw.exe").resolve()
>>> p
WindowsPath('C:/Python27/pythonw.exe')
>>> str(p)
'C:\\Python27\\pythonw.exe'

Hoặc trên Unix:

>>> from pathlib import Path
>>> p = Path("python3.4").resolve()
>>> p
PosixPath('/opt/python3/bin/python3.4')
>>> str(p)
'/opt/python3/bin/python3.4'

Tài liệu ở đây: https://docs.python.org/3/l Library/pathlib.html

Đã trả lời ngày 24 tháng 10 năm 2014 lúc 1:05Oct 24, 2014 at 1:05Oct 24, 2014 at 1:05

Twasbrilligtwasbrilligtwasbrilligtwasbrillig

15,5k9 huy hiệu vàng40 Huy hiệu bạc62 Huy hiệu đồng9 gold badges40 silver badges62 bronze badges9 gold badges40 silver badges62 bronze badges

4

import os
os.path.abspath(os.path.expanduser(os.path.expandvars(PathNameString)))

Lưu ý rằng

>>> import os
>>> os.path.abspath("C:/example/cwd/mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
2 là cần thiết (trên UNIX) trong trường hợp biểu thức đã cho cho tên và vị trí của tệp (hoặc thư mục) có thể chứa hàng đầu ____ 23 (Tilde đề cập đến thư mục nhà của người dùng) và
>>> import os
>>> os.path.abspath("C:/example/cwd/mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
4 chăm sóc bất kỳ biến môi trường nào khác ( như
>>> import os
>>> os.path.abspath("C:/example/cwd/mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
5).

Đã trả lời ngày 7 tháng 3 năm 2019 lúc 0:56Mar 7, 2019 at 0:56Mar 7, 2019 at 0:56

BENJIMINBENJIMINbenjiminbenjimin

3.34025 Huy hiệu bạc39 Huy hiệu đồng25 silver badges39 bronze badges25 silver badges39 bronze badges

1

Cài đặt mô-đun đường dẫn của bên thứ ba (được tìm thấy trên

>>> import os
>>> os.path.abspath("C:/example/cwd/mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
6), nó kết thúc tất cả các chức năng
>>> import os
>>> os.path.abspath("C:/example/cwd/mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
7 và các chức năng liên quan khác vào các phương thức trên một đối tượng có thể được sử dụng bất cứ nơi nào các chuỗi được sử dụng:
"C:/example/cwd/mydir/myfile.txt"
3

wim

313K95 Huy hiệu vàng571 Huy hiệu bạc710 Huy hiệu đồng95 gold badges571 silver badges710 bronze badges95 gold badges571 silver badges710 bronze badges

Đã trả lời ngày 12 tháng 9 năm 2008 lúc 6:53Sep 12, 2008 at 6:53Sep 12, 2008 at 6:53

TomtomTomTom

40.9K33 Huy hiệu vàng92 Huy hiệu bạc101 Huy hiệu đồng33 gold badges92 silver badges101 bronze badges33 gold badges92 silver badges101 bronze badges

5

Cập nhật cho Python 3.4+

>>> import os
>>> os.path.abspath("C:/example/cwd/mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
0 thực sự trả lời câu hỏi:
"C:/example/cwd/mydir/myfile.txt"
5

Nếu bạn chỉ cần một chuỗi tạm thời, hãy nhớ rằng bạn có thể sử dụng các đối tượng

>>> import os
>>> os.path.abspath("C:/example/cwd/mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
9 với tất cả các chức năng có liên quan trong
>>> import os
>>> os.path.abspath("C:/example/cwd/mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
7, bao gồm tất nhiên
>>> from pathlib import Path
>>> p = Path("pythonw.exe").resolve()
>>> p
WindowsPath('C:/Python27/pythonw.exe')
>>> str(p)
'C:\\Python27\\pythonw.exe'
1:
"C:/example/cwd/mydir/myfile.txt"
9

Đã trả lời ngày 27 tháng 12 năm 2018 lúc 20:41Dec 27, 2018 at 20:41Dec 27, 2018 at 20:41

Nhà vật lý vật lý điênMad PhysicistMad Physicist

101K24 Huy hiệu vàng165 Huy hiệu bạc248 Huy hiệu Đồng24 gold badges165 silver badges248 bronze badges24 gold badges165 silver badges248 bronze badges

Điều này luôn có tên tệp phù hợp của tập lệnh hiện tại, ngay cả khi nó được gọi từ trong một tập lệnh khác. Nó đặc biệt hữu ích khi sử dụng

>>> from pathlib import Path
>>> p = Path("pythonw.exe").resolve()
>>> p
WindowsPath('C:/Python27/pythonw.exe')
>>> str(p)
'C:\\Python27\\pythonw.exe'
2.always gets the right filename of the current script, even when it is called from within another script. It is especially useful when using
>>> from pathlib import Path
>>> p = Path("pythonw.exe").resolve()
>>> p
WindowsPath('C:/Python27/pythonw.exe')
>>> str(p)
'C:\\Python27\\pythonw.exe'
2.
>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
2always gets the right filename of the current script, even when it is called from within another script. It is especially useful when using
>>> from pathlib import Path
>>> p = Path("pythonw.exe").resolve()
>>> p
WindowsPath('C:/Python27/pythonw.exe')
>>> str(p)
'C:\\Python27\\pythonw.exe'
2.
>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
2

Từ đó, bạn có thể nhận được đường dẫn đầy đủ của tập lệnh với:

>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
0

Nó cũng giúp điều hướng các thư mục dễ dàng hơn bằng cách chỉ cần thêm

>>> from pathlib import Path
>>> p = Path("pythonw.exe").resolve()
>>> p
WindowsPath('C:/Python27/pythonw.exe')
>>> str(p)
'C:\\Python27\\pythonw.exe'
3 nhiều lần như bạn muốn đi 'lên' trong hệ thống phân cấp của các thư mục.

Để có được CWD:

>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
1

Đối với con đường cha mẹ:

>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
2

Bằng cách kết hợp

>>> from pathlib import Path
>>> p = Path("pythonw.exe").resolve()
>>> p
WindowsPath('C:/Python27/pythonw.exe')
>>> str(p)
'C:\\Python27\\pythonw.exe'
4 với các tên tệp khác, bạn có thể truy cập bất kỳ tệp nào trong hệ thống.

Đã trả lời ngày 28 tháng 2 năm 2019 lúc 15:26Feb 28, 2019 at 15:26Feb 28, 2019 at 15:26

3

Bạn có thể sử dụng điều này để có được đường dẫn tuyệt đối của một tệp cụ thể.

>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
3

Đã trả lời ngày 30 tháng 8 năm 2021 lúc 14:44Aug 30, 2021 at 14:44Aug 30, 2021 at 14:44

1

Nếu bạn đang ở trên máy Mac

>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
4

Điều này sẽ cung cấp cho bạn một con đường đầy đủ:

>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
5

sẽ hiển thị đường dẫn sau:

>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
6

Đã trả lời ngày 3 tháng 4 năm 2018 lúc 21:12Apr 3, 2018 at 21:12Apr 3, 2018 at 21:12

Chikwapurochikwapurochikwapurochikwapuro

1.2981 Huy hiệu vàng9 Huy hiệu bạc1 gold badge9 silver badges10 bronze badges1 gold badge9 silver badges10 bronze badges

1

Đưa ra một đường dẫn như

>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
9, làm thế nào để tôi tìm thấy đường dẫn tuyệt đối của tệp trong Python? Ví dụ. Trên Windows, tôi có thể kết thúc với:
"C:/example/cwd/mydir/myfile.txt"

hỏi ngày 9 tháng 9 năm 2008 lúc 10:19Sep 9, 2008 at 10:19

>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
7

Cũng hoạt động nếu nó đã là một con đường tuyệt đối:

>>> from pathlib import Path
>>> p = Path("pythonw.exe").resolve()
>>> p
WindowsPath('C:/Python27/pythonw.exe')
>>> str(p)
'C:\\Python27\\pythonw.exe'
6

Héo úaFeb 8 at 6:20

2.5831 Huy hiệu vàng19 Huy hiệu bạc28 Huy hiệu đồng1 gold badge19 silver badges28 bronze badgesEvan Carroll

Đã trả lời ngày 9 tháng 9 năm 2008 lúc 10:21Sep 9, 2008 at 10:2144 gold badges243 silver badges425 bronze badges

Sherbangsherbangsherbang

>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
8

Đã trả lời ngày 4 tháng 7 năm 2018 lúc 19:09Jul 4, 2018 at 19:09Jul 4, 2018 at 19:09

BndbndBNDBND

5629 Huy hiệu bạc21 Huy hiệu Đồng9 silver badges21 bronze badges9 silver badges21 bronze badges

Làm thế nào để bạn tìm thấy con đường tuyệt đối từ một con đường tương đối?

Để tìm đường dẫn tuyệt đối đầy đủ của thư mục hiện tại, hãy sử dụng lệnh PWD. Khi bạn đã xác định đường dẫn đến thư mục hiện tại, đường dẫn tuyệt đối đến tệp là đường dẫn cộng với tên của tệp. Ví dụ: nếu trong thư mục CGI-bin, chúng tôi đã có một tệp gọi là "ví dụ.use the pwd command. Once you've determined the path to the current directory, the absolute path to the file is the path plus the name of the file. For example, if in the cgi-bin directory we had a file called "example.use the pwd command. Once you've determined the path to the current directory, the absolute path to the file is the path plus the name of the file. For example, if in the cgi-bin directory we had a file called "example.

Làm thế nào để bạn chuyển đổi đường dẫn tương đối sang đường dẫn tuyệt đối trong Python?

path.relpath (đường dẫn) tạo ra một đường dẫn tuyệt đối đến đường dẫn tương đối.Và nếu đường dẫn được cung cấp là một đường dẫn tương đối thì hàm trả về cùng một đường dẫn.relpath(path) makes a absolute path to relative path. And if the path provided is itself a relative path then the function returns the same path. relpath(path) makes a absolute path to relative path. And if the path provided is itself a relative path then the function returns the same path.

Làm thế nào để bạn tìm thấy con đường tuyệt đối của một con đường trong Python?

Để có được một đường dẫn tuyệt đối trong Python, bạn sử dụng thư viện OS.Path.abspath.Chèn tên tệp của bạn và nó sẽ trả về đường dẫn đầy đủ từ thư mục làm việc bao gồm cả tệp.use the os.path.abspath library. Insert your file name and it will return the full path relative from the working directory including the file.use the os. path. abspath library. Insert your file name and it will return the full path relative from the working directory including the file.

Làm thế nào để bạn truy cập vào đường dẫn tương đối trong Python?

Một đường dẫn tương đối bắt đầu với/, ./ hoặc ../.Để có được một đường dẫn tương đối trong Python, trước tiên bạn phải tìm vị trí của thư mục làm việc nơi tập lệnh hoặc mô -đun được lưu trữ. Sau đó, bạn có được đường dẫn tương đối đến tệp muốn.find the location of the working directory where the script or module is stored.Then from that location, you get the relative path to the file want.find the location of the working directory where the script or module is stored. Then from that location, you get the relative path to the file want.