29
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.
Làm thế nào để mở một tệp trong thư mục cha trong Python trong appengine?
Tôi có một mô -đun/mod.py tệp Python với mã sau
f = open['../data.yml']
z = yaml.load[f]
f.close[]
data.yml nằm trong cha mẹ của mô -đun. Lỗi tôi nhận được là
IOError: [Errno 13] file not accessible: '../data.yml'
Tôi đang sử dụng appengine sdk 1.3.3.
Có một cách giải quyết cho điều này?
Khi được hỏi ngày 2 tháng 5 năm 2010 lúc 11:09May 2, 2010 at 11:09
Hàm open
hoạt động liên quan đến thư mục làm việc quy trình hiện tại, không phải mô -đun được gọi từ. Nếu đường dẫn phải liên quan đến mô-đun, hãy làm điều này:
import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
Đã trả lời ngày 2 tháng 5 năm 2010 lúc 11:29May 2, 2010 at 11:29
Marcelo Cantosmarcelo CantosMarcelo Cantos
177K38 Huy hiệu vàng321 Huy hiệu bạc362 Huy hiệu Đồng38 gold badges321 silver badges362 bronze badges
4
Đã gặp phải câu hỏi này và không hài lòng với câu trả lời, tôi đã chạy qua một giải pháp khác. Nó đã mất như sau để có được những gì tôi muốn.
Xác định thư mục hiện tại bằng cách sử dụng
os.path.dirname
:current_directory = os.path.dirname[__file__]
Xác định thư mục cha mẹ bằng cách sử dụng
0:IOError: [Errno 13] file not accessible: '../data.yml'
1IOError: [Errno 13] file not accessible: '../data.yml'
Tham gia Parent_Directory với bất kỳ Bộ phận con nào:
2IOError: [Errno 13] file not accessible: '../data.yml'
Mở tập tin:
3IOError: [Errno 13] file not accessible: '../data.yml'
Kết hợp với nhau:
open[os.path.join[os.path.split[os.path.dirname[__file__]][0], 'path', 'to', 'file']
Đã trả lời ngày 17 tháng 9 năm 2015 lúc 5:46Sep 17, 2015 at 5:46
Giải pháp thay thế 🔰🔰
Bạn cũng có thể sử dụng mô -đun
IOError: [Errno 13] file not accessible: '../data.yml'
4 để có thư mục làm việc hiện tại. Do đó, một sự thay thế khác để làm điều tương tự sẽ là:Thus, another alternative to do the same thing would be:
import sys
f = open[sys.path[0] + '/../data.yml']
Đã trả lời ngày 9 tháng 2 năm 2021 lúc 10:44Feb 9, 2021 at 10:44
Anjandashanjandashanjandash
7378 Huy hiệu bạc20 Huy hiệu Đồng8 silver badges20 bronze badges
Tôi đã viết một hàm nhỏ gọi là get_parent_directory [] có thể giúp nhận được đường dẫn của thư mục cha:
import sys
def get_parent_directory[]:
list = sys.path[0].split['/'][:-1]
return_str = ''
for element in list:
return_str += element + '/'
return return_str.rstrip['/']
Đã trả lời ngày 19 tháng 8 năm 2021 lúc 14:15Aug 19, 2021 at 14:15
1
@ThatsArArais Trả lời trong một chức năng
import os
def getParent[path: str, levels=1] -> str:
"""
@param path: starts without /
@return: Parent path at the specified levels above.
"""
current_directory = os.path.dirname[__file__]
parent_directory = current_directory
for i in range[0, levels]:
parent_directory = os.path.split[parent_directory][0]
file_path = os.path.join[parent_directory, path]
return file_path
Đã trả lời ngày 29 tháng 12 năm 2021 lúc 8:00Dec 29, 2021 at 8:00
Tonisivestonisivestonisives
1.6151 Huy hiệu vàng15 Huy hiệu bạc16 Huy hiệu đồng1 gold badge15 silver badges16 bronze badges
Trong Python, mô -đun HĐH được sử dụng để tương tác với hệ điều hành. Nó nằm trong các mô -đun tiện ích tiêu chuẩn Python. Mô -đun này cung cấp một cách di động để sử dụng chức năng phụ thuộc hệ điều hành. Các mô -đun * OS * và * OS.PATH * bao gồm nhiều chức năng để tương tác với hệ thống tệp. & NBSP; Mô -đun OS cung cấp nhiều cách khác nhau để có được thư mục cha. Một số cách là: & nbsp;OS module is used to interact with the operating system. It comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. The *os* and *os.path* modules include many functions to interact with the file system.
OS module provides various ways for getting the parent directory. Some of the ways are:
- Sử dụng os.path.abspath [] & nbsp;
- Sử dụng os.path.dirname [] & nbsp;
- Sử dụng OS.Path.RelPath [] và Os.Path.DirName []
- Sử dụng Path []. Resolve []. Cha mẹ
Sử dụng OS.Path.Abspath [] để lấy cha mẹ của thư mục hiện tại
os.path.abspath [] có thể được sử dụng để lấy thư mục mẹ. Phương pháp này được sử dụng để có được phiên bản chuẩn hóa của đường dẫn. Hàm này cũng cần sự trợ giúp của Os.Path.Join [] và Os.Pardir []. & NBSP; Os.Path.Join [] Phương thức trong Python tham gia một hoặc nhiều thành phần đường dẫn một cách thông minh. Phương pháp này kết hợp các thành phần đường dẫn khác nhau với chính xác một dấu phân tách thư mục [‘/,] theo từng phần không trống ngoại trừ thành phần đường dẫn cuối cùng. Nếu thành phần đường dẫn cuối cùng được nối là trống thì một dấu phân cách thư mục [‘//] sẽ được đặt ở cuối. & Nbsp;
os.path.join[] method in Python join one or more path components intelligently. This method concatenates various path components with exactly one directory separator [‘/’] following each non-empty part except the last path component. If the last path component to be joined is empty then a
directory separator [‘/’] is put at the end.
Cú pháp: Os.Path.Abpath [Path] Tham số: & nbsp; đường dẫn: một đối tượng giống như đường dẫn biểu thị đường dẫn hệ thống tệp. Loại return: Trả về một chuỗi là phiên bản chuẩn hóa của đường dẫn. & NBSP; os.path.abspath[path]
Parameters:
path: A path-like object representing a file system path.
Return Type: Returns a string that is a normalized version of the path.
Example:
Python3
IOError: [Errno 13] file not accessible: '../data.yml'
5 IOError: [Errno 13] file not accessible: '../data.yml'
6IOError: [Errno 13] file not accessible: '../data.yml'
7IOError: [Errno 13] file not accessible: '../data.yml'
8 IOError: [Errno 13] file not accessible: '../data.yml'
9import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
0import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
1import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
2223import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
0import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
5Output:
Sử dụng OS.Path.Dirname [] để lấy cha mẹ của thư mục hiện tại
Phương thức OS.Path.DirName [] trong Python được sử dụng để lấy tên thư mục từ đường dẫn được chỉ định. & NBSP;
Cú pháp: Os.Path.Dirname [Path] Tham số: & nbsp; đường dẫn: Một đối tượng giống như đường dẫn biểu thị đường dẫn hệ thống tệp. Loại return: Phương thức này trả về một giá trị chuỗi đại diện cho tên thư mục từ đường dẫn được chỉ định. & NBSP;os.path.dirname[path]
Parameter:
path: A path-like object representing a file system path.
Return Type: This method returns a string value which represents the directory name from the specified path.
Example:
Python3
IOError: [Errno 13] file not accessible: '../data.yml'
5 IOError: [Errno 13] file not accessible: '../data.yml'
6IOError: [Errno 13] file not accessible: '../data.yml'
7IOError: [Errno 13] file not accessible: '../data.yml'
8 IOError: [Errno 13] file not accessible: '../data.yml'
9import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
0import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
1import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
2import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
3import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
0import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
1import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
2223Sử dụng OS.Path.Dirname [] để lấy cha mẹ của thư mục hiện tại
import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
0import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
1import sys
f = open[sys.path[0] + '/../data.yml']
2import sys
f = open[sys.path[0] + '/../data.yml']
3Output:
Sử dụng OS.Path.RelPath [] và Os.Path.DirName []
Sử dụng Path []. Resolve []. Cha mẹ
os.path.relpath[] method in Python is used to get a relative filepath to the given path either from the current working directory or from the given directory.
Sử dụng OS.Path.Abspath [] để lấy cha mẹ của thư mục hiện tạios.path.relpath[path, start = os.curdir]
Parameter:
- os.path.abspath [] có thể được sử dụng để lấy thư mục mẹ. Phương pháp này được sử dụng để có được phiên bản chuẩn hóa của đường dẫn. Hàm này cũng cần sự trợ giúp của Os.Path.Join [] và Os.Pardir []. & NBSP; Os.Path.Join [] Phương thức trong Python tham gia một hoặc nhiều thành phần đường dẫn một cách thông minh. Phương pháp này kết hợp các thành phần đường dẫn khác nhau với chính xác một dấu phân tách thư mục [‘/,] theo từng phần không trống ngoại trừ thành phần đường dẫn cuối cùng. Nếu thành phần đường dẫn cuối cùng được nối là trống thì một dấu phân cách thư mục [‘//] sẽ được đặt ở cuối. & Nbsp; A path-like object representing the file system path.
- Cú pháp: Os.Path.Abpath [Path] Tham số: & nbsp; đường dẫn: một đối tượng giống như đường dẫn biểu thị đường dẫn hệ thống tệp. Loại return: Trả về một chuỗi là phiên bản chuẩn hóa của đường dẫn. & NBSP; A path-like object representing the file system path.
The relative path for given path will be computed with respect to the directory indicated by start. The default value of this parameter is os.curdir which is a constant string used by the operating system to refer to the current directory.
5IOError: [Errno 13] file not accessible: '../data.yml'
6This method returns a string value which represents the relative file path to given path from the start directory.0222IOError: [Errno 13] file not accessible: '../data.yml'
IOError: [Errno 13] file not accessible: '../data.yml'
7IOError: [Errno 13] file not accessible: '../data.yml'
8 IOError: [Errno 13] file not accessible: '../data.yml'
9Example:
Python3
import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
0import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
1import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
2223Sử dụng OS.Path.Dirname [] để lấy cha mẹ của thư mục hiện tại
Phương thức OS.Path.DirName [] trong Python được sử dụng để lấy tên thư mục từ đường dẫn được chỉ định. & NBSP;
Cú pháp: Os.Path.Dirname [Path] Tham số: & nbsp; đường dẫn: Một đối tượng giống như đường dẫn biểu thị đường dẫn hệ thống tệp. Loại return: Phương thức này trả về một giá trị chuỗi đại diện cho tên thư mục từ đường dẫn được chỉ định. & NBSP;
import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
0open[os.path.join[os.path.split[os.path.dirname[__file__]][0], 'path', 'to', 'file']
6import sys
def get_parent_directory[]:
list = sys.path[0].split['/'][:-1]
return_str = ''
for element in list:
return_str += element + '/'
return return_str.rstrip['/']
1import os
def getParent[path: str, levels=1] -> str:
"""
@param path: starts without /
@return: Parent path at the specified levels above.
"""
current_directory = os.path.dirname[__file__]
parent_directory = current_directory
for i in range[0, levels]:
parent_directory = os.path.split[parent_directory][0]
file_path = os.path.join[parent_directory, path]
return file_path
9 open
0IOError: [Errno 13] file not accessible: '../data.yml'
7IOError: [Errno 13] file not accessible: '../data.yml'
8 open
3import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
0open
5open
6open
7Output:
Sử dụng Path []. Resolve []. Cha mẹ để có được cha mẹ của thư mục hiện tại
Cú pháp: Đường dẫn [đường dẫn] .Resolve []. Cha mẹ [0] Path[path].resolve[].parents[0]
Parameter:
- Đường dẫn: Đường dẫn của tệp hoặc thư mục có cha mẹ mà chúng tôi muốn tìm nạp.path of the file or folder whose parent we want to fetch.
Loại trả về: Nó trả về WindowsPath.Parents Object.It returns WindowsPath.parents object.
Ở đây chúng tôi đang sử dụng mô -đun đường dẫn để có được cha mẹ của thư mục hiện tại và thay vì cha mẹ [0] chúng tôi cũng có thể sử dụng cha mẹ. Tờ để lấy cha mẹ của thư mục hiện tại.
Example:
Python3
open
8 open
9
IOError: [Errno 13] file not accessible: '../data.yml'
5 os.path.dirname
1os.path.dirname
2
IOError: [Errno 13] file not accessible: '../data.yml'
8 os.path.dirname
4os.path.dirname
5os.path.dirname
6os.path.dirname
7os.path.dirname
8import os.path
f = open[os.path.dirname[__file__] + '/../data.yml']
0current_directory = os.path.dirname[__file__]
0Output: