Hướng dẫn how do i open a file in python path? - làm cách nào để mở một tệp trong đường dẫn python?

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 how do i open a file in python path? - làm cách nào để mở một tệp trong đường dẫn python?

Tế bào thần kinh

4.6824 Huy hiệu vàng33 Huy hiệu bạc54 Huy hiệu Đồng4 gold badges33 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 how do i open a file in python path? - làm cách nào để mở một tệp trong đường dẫn python?

Tế bào thần kinh

4.6824 Huy hiệu vàng33 Huy hiệu bạc54 Huy hiệu Đồng4 gold badges33 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 badges31 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:
    <do stuff>

Đâ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:
    <do stuff>

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.5275 Huy hiệu vàng31 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 how do i open a file in python path? - làm cách nào để mở một tệp trong đường dẫn python?

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 badges32 silver badges35 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.0152 Huy hiệu vàng32 Huy hiệu bạc35 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 badges350 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 badges14 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 how do i open a file in python path? - làm cách nào để mở một tệp trong đường dẫn python?

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 how do i open a file in python path? - làm cách nào để mở một tệp trong đường dẫn python?

30.5K37 Huy hiệu vàng174 Huy hiệu bạc350 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

3553 Huy hiệu bạc14 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.12722 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 badges300 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

173K34 Huy hiệu vàng268 Huy hiệu bạc300 Huy hiệu đồ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)
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 how do i open a file in python path? - làm cách nào để mở một tệp trong đường dẫn python?

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 how do i open a file in python path? - làm cách nào để mở một tệp trong đường dẫn python?

NeuronneuronNeuron

4.6824 Huy hiệu vàng33 Huy hiệu bạc54 Huy hiệu Đồng4 gold badges33 silver badges54 bronze badges

Làm cách nào để mở đường dẫn tệp?

Để xem đường dẫn đầy đủ của một tệp riêng lẻ:..
Nhấp vào nút Bắt đầu và sau đó nhấp vào Máy tính, nhấp để mở vị trí của tệp mong muốn, giữ phím Shift và nhấp chuột phải vào tệp ..
Trên menu, có hai tùy chọn để chọn từ đó sẽ cho phép bạn sao chép hoặc xem toàn bộ đường dẫn tệp:.

Làm cách nào để đặt đường dẫn của một tệp trong Python?

Tham khảo một tệp trong Windows..
Python cho phép bạn sử dụng dấu gạch chéo kiểu OS-X/Linux "/" ngay cả trong Windows.....
Nếu sử dụng Backslash, vì nó là một nhân vật đặc biệt trong Python, bạn phải nhớ thoát khỏi mọi phiên bản: 'C: \\ users \\ narae \\ Desktop \\ alice ..

Làm thế nào để bạn truy cập một tệp trong Python?

Có 6 chế độ truy cập trong Python ...
Chỉ đọc ('r'): Mở tệp văn bản để đọc.....
Đọc và viết ('R+'): Mở tệp để đọc và viết.....
Chỉ viết ('W'): Mở tệp để viết.....
Viết và đọc ('W+'): Mở tệp để đọc và viết.....
Chỉ nối thêm ('A'): Mở tệp để viết ..