Hướng dẫn open file python with path - mở tệp python với đường dẫn

Giả sử mã Python của tôi được thực thi một thư mục gọi là

import os

def read_file(file_name):
    file_handle = open(file_name)
    print file_handle.read()
    file_handle.close()

file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir

#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)

#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)

#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)

#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
5 và ứng dụng cần truy cập
import os

def read_file(file_name):
    file_handle = open(file_name)
    print file_handle.read()
    file_handle.close()

file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir

#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)

#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)

#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)

#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
6.

Tôi nên sử dụng

import os

def read_file(file_name):
    file_handle = open(file_name)
    print file_handle.read()
    file_handle.close()

file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir

#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)

#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)

#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)

#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
7 như thế nào? Tham số
import os

def read_file(file_name):
    file_handle = open(file_name)
    print file_handle.read()
    file_handle.close()

file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir

#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)

#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)

#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)

#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
8 nên là gì?

Tôi thấy rằng dưới đây mã đơn giản sẽ hoạt động .. Nó có bất kỳ nhược điểm nào không?

file = "\2091\sample.txt"
path = os.getcwd()+file
fp = open(path, 'r+');

Hướng dẫn open file python with path - mở tệp python với đường dẫn

Tế bào thần kinh

4.7204 Huy hiệu vàng34 Huy hiệu bạc54 Huy hiệu đồng4 gold badges34 silver badges54 bronze badges

Hỏi ngày 23 tháng 8 năm 2011 lúc 18:24Aug 23, 2011 at 18:24

4

Với loại điều này, bạn cần phải cẩn thận với thư mục làm việc thực tế của bạn là gì. Ví dụ: bạn không được chạy tập lệnh từ thư mục tệp đang ở. Trong trường hợp này, bạn không thể chỉ sử dụng một đường dẫn tương đối.

Nếu bạn chắc chắn rằng tệp bạn muốn nằm trong một thư mục con bên dưới nơi tập lệnh thực sự được đặt, bạn có thể sử dụng

import os

def read_file(file_name):
    file_handle = open(file_name)
    print file_handle.read()
    file_handle.close()

file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir

#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)

#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)

#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)

#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
9 để giúp bạn ra khỏi đây.
import os

def read_file(file_name):
    file_handle = open(file_name)
    print file_handle.read()
    file_handle.close()

file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir

#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)

#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)

#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)

#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
9 là đường dẫn đầy đủ đến nơi tập lệnh bạn đang chạy.

Vì vậy, bạn có thể đánh bại một cái gì đó như thế này:

import os
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)

Đã trả lời ngày 23 tháng 8 năm 2011 lúc 18:59Aug 23, 2011 at 18:59

NgaRuss

10,4K12 Huy hiệu vàng41 Huy hiệu bạc55 Huy hiệu Đồng12 gold badges41 silver badges55 bronze badges

6

Mã này hoạt động tốt:

import os

def read_file(file_name):
    file_handle = open(file_name)
    print file_handle.read()
    file_handle.close()

file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir

#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)

#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)

#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)

#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)

Hướng dẫn open file python with path - mở tệp python với đường dẫn

Tế bào thần kinh

4.7204 Huy hiệu vàng34 Huy hiệu bạc54 Huy hiệu đồng4 gold badges34 silver badges54 bronze badges

Hỏi ngày 23 tháng 8 năm 2011 lúc 18:24Oct 6, 2015 at 15:06

4

Với loại điều này, bạn cần phải cẩn thận với thư mục làm việc thực tế của bạn là gì. Ví dụ: bạn không được chạy tập lệnh từ thư mục tệp đang ở. Trong trường hợp này, bạn không thể chỉ sử dụng một đường dẫn tương đối.

Nếu bạn chắc chắn rằng tệp bạn muốn nằm trong một thư mục con bên dưới nơi tập lệnh thực sự được đặt, bạn có thể sử dụng

import os

def read_file(file_name):
    file_handle = open(file_name)
    print file_handle.read()
    file_handle.close()

file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir

#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)

#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)

#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)

#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
9 để giúp bạn ra khỏi đây.
import os

def read_file(file_name):
    file_handle = open(file_name)
    print file_handle.read()
    file_handle.close()

file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir

#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)

#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)

#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)

#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
9 là đường dẫn đầy đủ đến nơi tập lệnh bạn đang chạy.

import os
script_dir = os.path.dirname(__file__)
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)

Vì vậy, bạn có thể đánh bại một cái gì đó như thế này:

Đã trả lời ngày 23 tháng 8 năm 2011 lúc 18:59

Nga

import os
script_path = os.path.abspath(__file__) # i.e. /path/to/dir/foobar.py
script_dir = os.path.split(script_path)[0] #i.e. /path/to/dir/
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)

10,4K12 Huy hiệu vàng41 Huy hiệu bạc55 Huy hiệu Đồng

Mã này hoạt động tốt:

Đã trả lời ngày 6 tháng 10 năm 2015 lúc 15:065 gold badges33 silver badges41 bronze badges

Tôi đã tạo một tài khoản chỉ để tôi có thể làm rõ sự khác biệt mà tôi nghĩ rằng tôi đã tìm thấy trong phản hồi ban đầu của Nga.Sep 1, 2014 at 20:54

2

Để tham khảo, câu trả lời ban đầu của anh ấy là:

from os import path

file_path = path.relpath("2091/data.txt")
with open(file_path) as f:
    

Đây là một câu trả lời tuyệt vời bởi vì nó đang cố gắng tự động tạo ra một đường dẫn hệ thống tuyệt đối đến tệp mong muốn.

Cory Mawhorter nhận thấy rằng

import os

def read_file(file_name):
    file_handle = open(file_name)
    print file_handle.read()
    file_handle.close()

file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir

#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)

#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)

#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)

#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
9 là một đường dẫn tương đối (nó cũng nằm trên hệ thống của tôi) và đề xuất sử dụng
import os
script_dir = os.path.dirname(__file__)
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
2.
import os
script_dir = os.path.dirname(__file__)
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
3, tuy nhiên, trả về đường dẫn tuyệt đối của tập lệnh hiện tại của bạn (tức là
import os
script_dir = os.path.dirname(__file__)
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
4)

Edit::

Để sử dụng phương pháp này (và cuối cùng tôi đã hoạt động như thế nào), bạn phải xóa tên tập lệnh từ cuối đường dẫn:

with open("2091/data/txt") as f:
    

Kết quả ABS_FILE_PATH (trong ví dụ này) trở thành:

import os
script_dir = os.path.dirname(__file__)
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
5

FerguzzAug 23, 2011 at 18:30

5.5875 Huy hiệu vàng33 Huy hiệu bạc41 Huy hiệu đồngWilduck

Đã trả lời ngày 1 tháng 9 năm 2014 lúc 20:5410 gold badges57 silver badges89 bronze badges

5

Nó phụ thuộc vào hệ điều hành bạn đang sử dụng. Nếu bạn muốn một giải pháp tương thích với cả Windows và *NIX, một cái gì đó như:

import os

script_dir = os.path.dirname(__file__)
file_path = os.path.join(script_dir, './output03.txt')
print(file_path)
fptr = open(file_path, 'w')

Nên làm việc tốt.Sep 1, 2018 at 22:14

Hướng dẫn open file python with path - mở tệp python với đường dẫn

Mô -đun

import os
script_dir = os.path.dirname(__file__)
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
6 có thể định dạng một đường dẫn cho bất kỳ hệ điều hành nào mà nó đang chạy. Ngoài ra, Python xử lý các đường dẫn tương đối tốt, miễn là bạn có quyền chính xác.Ângelo Polotto

Như đã đề cập bởi Kindall trong các bình luận, Python có thể chuyển đổi giữa các đường dẫn kiểu UNIX và Windows, do đó, thậm chí mã đơn giản hơn sẽ hoạt động:2 gold badges34 silver badges36 bronze badges

4

Điều đó đang được nói, mô -đun

import os
script_dir = os.path.dirname(__file__)
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
6 vẫn có một số chức năng hữu ích.

from pathlib import Path

data_folder = Path("/relative/path")
file_to_open = data_folder / "file.pdf"

f = open(file_to_open)

print(f.read())

Đã trả lời ngày 23 tháng 8 năm 2011 lúc 18:30

WilduckwilduckAug 3, 2018 at 10:57

13.6K10 Huy hiệu vàng57 Huy hiệu bạc89 Huy hiệu đồngdcampos

Tôi dành nhiều thời gian để khám phá lý do tại sao mã của tôi không thể tìm thấy tệp của tôi chạy Python 3 trên hệ thống Windows. Vì vậy, tôi đã thêm. Trước / và mọi thứ hoạt động tốt:1 silver badge6 bronze badges

1

Code:

import os
script_path = os.path.abspath(__file__) 
path_list = script_path.split(os.sep)
script_directory = path_list[0:len(path_list)-1]
rel_path = "main/2091/data.txt"
path = "/".join(script_directory) + "/" + rel_path

Explanation:

Đã trả lời ngày 1 tháng 9 năm 2018 lúc 22:14

import os
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
0

Ângelo polottoângelo polotto

import os
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
1

7.2352 Huy hiệu vàng34 Huy hiệu bạc36 Huy hiệu đồng

import os
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
2

Thử cái này:

import os
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
3

Python 3.4 đã giới thiệu một thư viện tiêu chuẩn mới để xử lý các tệp và đường dẫn được gọi là pathlib. Nó hoạt động cho tôi!

import os
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
4

Đã trả lời ngày 3 tháng 8 năm 2018 lúc 10:57

import os
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
5

Dcamposdcampos

import os
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
6

Mr_and_Mrs_D

1291 Huy hiệu bạc6 Huy hiệu đồng37 gold badges174 silver badges353 bronze badges

Thư viện nhập khẩu:Dec 22, 2018 at 22:09

Patriots299Patriots299Patriots299

Sử dụng

import os

def read_file(file_name):
    file_handle = open(file_name)
    print file_handle.read()
    file_handle.close()

file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir

#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)

#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)

#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)

#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
9 để đạt được đường dẫn của tập lệnh hiện tại:3 silver badges15 bronze badges

1

import os
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
7

Tách đường dẫn tập lệnh thành nhiều mục:Dec 8, 2019 at 15:02

Hướng dẫn open file python with path - mở tệp python với đường dẫn

Xóa mục cuối cùng trong danh sách (tệp tập lệnh thực tế):Webucator

Thêm đường dẫn của tệp tương đối:22 silver badges32 bronze badges

Tham gia các mục danh sách và thêm tệp của đường dẫn tương đối:

Bây giờ bạn được thiết lập để làm bất cứ điều gì bạn muốn với tệp, chẳng hạn như, ví dụ:Dec 30, 2017 at 15:04

Hướng dẫn open file python with path - mở tệp python với đường dẫn

30.7K37 Huy hiệu vàng174 Huy hiệu bạc353 Huy hiệu đồngOlivia Liao

Đã trả lời ngày 22 tháng 12 năm 2018 lúc 22:093 silver badges7 bronze badges

0

3653 Huy hiệu bạc15 Huy hiệu Đồng

Đã trả lời ngày 8 tháng 12 năm 2019 lúc 15:02

WebucatorWebucatorAug 23, 2011 at 18:31

2.14722 huy hiệu bạc32 huy hiệu đồngkindall

Nếu tệp nằm trong thư mục mẹ của bạn, ví dụ: follower.txt, bạn chỉ có thể sử dụng

import os
script_dir = os.path.dirname(__file__)
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
934 gold badges268 silver badges301 bronze badges

3

Đã trả lời ngày 30 tháng 12 năm 2017 lúc 15:04

Olivia Liaolivia Liao

3653 Huy hiệu bạc7 Huy hiệu đồng

import os
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
8

Python chỉ chuyển tên tệp mà bạn cung cấp cho hệ điều hành, mở ra nó. Nếu hệ điều hành của bạn hỗ trợ các đường dẫn tương đối như

import os

def read_file(file_name):
    file_handle = open(file_name)
    print file_handle.read()
    file_handle.close()

file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir

#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)

#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)

#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)

#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
6 (gợi ý: nó có), thì điều đó sẽ hoạt động tốt.

import os
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
9

Bạn có thể thấy rằng cách dễ nhất để trả lời một câu hỏi như thế này là thử nó và xem điều gì sẽ xảy ra.Aug 21, 2014 at 2:20

Đã trả lời ngày 23 tháng 8 năm 2011 lúc 18:31Survi

Kindallkindall4 bronze badges

Phim thương hiệu vàng 174K34

import os

def read_file(file_name):
    file_handle = open(file_name)
    print file_handle.read()
    file_handle.close()

file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir

#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)

#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)

#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)

#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
0

Điều tương tự với

import os
script_path = os.path.abspath(__file__) # i.e. /path/to/dir/foobar.py
script_dir = os.path.split(script_path)[0] #i.e. /path/to/dir/
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
2:

import os

def read_file(file_name):
    file_handle = open(file_name)
    print file_handle.read()
    file_handle.close()

file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir

#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)

#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)

#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)

#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
1

Đã trả lời ngày 12 tháng 6 năm 2021 lúc 11:07Jun 12, 2021 at 11:07

James Wongjames WongJames Wong

371 Huy hiệu bạc5 Huy hiệu đồng1 silver badge5 bronze badges

2

Khi tôi là người mới bắt đầu, tôi thấy những mô tả này hơi đáng sợ.Như lúc đầu tôi sẽ thử

import os
script_path = os.path.abspath(__file__) # i.e. /path/to/dir/foobar.py
script_dir = os.path.split(script_path)[0] #i.e. /path/to/dir/
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
3
import os
script_path = os.path.abspath(__file__) # i.e. /path/to/dir/foobar.py
script_dir = os.path.split(script_path)[0] #i.e. /path/to/dir/
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
3

import os

def read_file(file_name):
    file_handle = open(file_name)
    print file_handle.read()
    file_handle.close()

file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir

#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)

#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)

#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)

#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
2

Và điều này sẽ tăng một

import os
script_path = os.path.abspath(__file__) # i.e. /path/to/dir/foobar.py
script_dir = os.path.split(script_path)[0] #i.e. /path/to/dir/
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
4.Tôi đã sử dụng bị nhầm lẫn rất nhiều.Sau đó sau khi lướt qua Google.tìm thấy lý do tại sao lỗi xảy ra.Viết cái này cho người mới bắt đầuWriting this for beginners

Đó là bởi vì đối với đường dẫn được đọc bằng unicode, bạn đơn giản thêm

import os
script_path = os.path.abspath(__file__) # i.e. /path/to/dir/foobar.py
script_dir = os.path.split(script_path)[0] #i.e. /path/to/dir/
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
5 khi bắt đầu đường dẫn tệp

import os

def read_file(file_name):
    file_handle = open(file_name)
    print file_handle.read()
    file_handle.close()

file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir

#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)

#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)

#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)

#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
3

Và bây giờ nó hoạt động chỉ cần thêm

import os
script_path = os.path.abspath(__file__) # i.e. /path/to/dir/foobar.py
script_dir = os.path.split(script_path)[0] #i.e. /path/to/dir/
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
5 trước khi bắt đầu thư mục.

Đã trả lời ngày 24 tháng 6 năm 2020 lúc 19:18Jun 24, 2020 at 19:18

Hướng dẫn open file python with path - mở tệp python với đường dẫn

1

Trong Python 3.4 (PEP 428),

import os
script_path = os.path.abspath(__file__) # i.e. /path/to/dir/foobar.py
script_dir = os.path.split(script_path)[0] #i.e. /path/to/dir/
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
2 đã được giới thiệu, cho phép bạn làm việc với các tệp theo cách định hướng đối tượng:

import os

def read_file(file_name):
    file_handle = open(file_name)
    print file_handle.read()
    file_handle.close()

file_dir = os.path.dirname(os.path.realpath('__file__'))
print file_dir

#For accessing the file in the same folder
file_name = "same.txt"
read_file(file_name)

#For accessing the file in a folder contained in the current folder
file_name = os.path.join(file_dir, 'Folder1.1/same.txt')
read_file(file_name)

#For accessing the file in the parent folder of the current folder
file_name = os.path.join(file_dir, '../same.txt')
read_file(file_name)

#For accessing the file inside a sibling folder.
file_name = os.path.join(file_dir, '../Folder2/same.txt')
file_name = os.path.abspath(os.path.realpath(file_name))
print file_name
read_file(file_name)
4

Từ khóa

import os
script_path = os.path.abspath(__file__) # i.e. /path/to/dir/foobar.py
script_dir = os.path.split(script_path)[0] #i.e. /path/to/dir/
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
8 cũng sẽ đảm bảo rằng tài nguyên của bạn sẽ bị đóng đúng cách, ngay cả khi bạn gặp phải sự cố (như một
import os
script_path = os.path.abspath(__file__) # i.e. /path/to/dir/foobar.py
script_dir = os.path.split(script_path)[0] #i.e. /path/to/dir/
rel_path = "2091/data.txt"
abs_file_path = os.path.join(script_dir, rel_path)
9, sigint hoặc tương tự)

Đã trả lời ngày 13 tháng 12 năm 2021 lúc 20:14Dec 13, 2021 at 20:14

Hướng dẫn open file python with path - mở tệp python với đường dẫn

NeuronneuronNeuron

4.7204 Huy hiệu vàng34 Huy hiệu bạc54 Huy hiệu đồng4 gold badges34 silver badges54 bronze badges