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

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__] #

Bài Viết Liên Quan

Chủ Đề