Đư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
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.6031 Huy hiệu vàng19 Huy hiệu bạc28 Huy hiệu đồng1 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:21 Sherbangsherbangsherbang Huy hiệu vàng 15.2K11 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:05
Twasbrilligtwasbrilligtwasbrillig 15.8k9 Huy hiệu vàng40 Huy hiệu bạc62 Huy hiệu Đồng9 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:56 BENJIMINBENJIMINbenjimin 3,45526 Huy hiệu bạc39 Huy hiệu đồng26 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:>>> from path import path
>>> path('mydir/myfile.txt').abspath()
'C:\\example\\cwd\\mydir\\myfile.txt'
wim 317K97 Huy hiệu vàng579 Huy hiệu bạc714 Huy hiệu đồng97 gold badges579 silver badges714 bronze badges Đã trả lời ngày 12 tháng 9 năm 2008 lúc 6:53Sep 12, 2008 at 6:53 TomtomTom 41.2K33 Huy hiệu vàng92 Huy hiệu bạc101 Huy hiệu đồng33 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:from pathlib import Path
relative = Path("mydir/myfile.txt")
absolute = relative.absolute() # absolute is a Path object
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:from os.path import abspath
absolute = abspath(relative) # absolute is a str object
Đã trả lời ngày 27 tháng 12 năm 2018 lúc 20:41Dec 27, 2018 at 20:41 Nhà vật lý vật lý điênMad Physicist 102K25 Huy hiệu vàng171 Huy hiệu bạc254 Huy hiệu đồng25 gold
badges171 silver badges254 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 sys,os
filename = sys.argv[0]
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'
0Nó 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'
2Bằ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: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:44 1 Đưa ra một đường dẫn như >>> from pathlib import Path
>>> p = Path("pythonw.exe").resolve()
>>> p
WindowsPath('C:/Python27/pythonw.exe')
>>> str(p)
'C:\\Python27\\pythonw.exe'
5, làm thế nào để tôi tìm thấy đường dẫn tuyệt đối của tệp liên quan đến thư mục làm việc hiện tại trong Python?
Tôi sẽ làm như thế này, >>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
4Mà trả về >>> from pathlib import Path
>>> p = Path("pythonw.exe").resolve()
>>> p
WindowsPath('C:/Python27/pythonw.exe')
>>> str(p)
'C:\\Python27\\pythonw.exe'
6 Đã trả lời ngày 8 tháng 2 lúc 6:20Feb 8 at 6:20 Evan Carrollevan CarrollEvan Carroll 74,5K45 Huy hiệu vàng245 Huy hiệu bạc434 Huy hiệu đồng45 gold badges245 silver
badges434 bronze badges Nếu bạn đang ở trên máy Mac >>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
5Đ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'
6sẽ hiển thị đường dẫn sau: >>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
7 Đã trả lời ngày 3 tháng 4 năm 2018 lúc 21:12Apr 3, 2018 at 21:12 Chikwapurochikwapurochikwapuro 1.3181 Huy hiệu vàng9 Huy hiệu bạc1 gold badge9 silver badges10 bronze badges 1 Trong trường hợp ai đó đang sử dụng Python và Linux và tìm kiếm toàn bộ đường dẫn đến tệp: >>> 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:09 BndbndBND 5729 Huy hiệu bạc21 Huy hiệu Đồng9 silver badges21 bronze badges
|