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

193

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.

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+'];

Tế bào thần kinh

4.6424 Huy hiệu vàng32 Huy hiệu bạc53 Huy hiệu Đồng4 gold badges32 silver badges53 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__] #

Bài Viết Liên Quan

Chủ Đề