Tôi đang nhận được một tệp đăng từ một tệp:
file = request.post['ufile']
Tôi muốn có được con đường. Làm thế nào tôi có thể nhận được nó?
HALFER
19.7K17 Huy hiệu vàng92 Huy hiệu bạc179 Huy hiệu đồng17 gold badges92 silver badges179 bronze badges
hỏi ngày 15 tháng 7 năm 2011 lúc 6:07Jul 15, 2011 at 6:07
RajasekarrajasekarRajasekar
Huy hiệu vàng 18K3232 gold badges101 silver badges136 bronze badges
Bạn phải sử dụng từ điển request.FILES
.
Kiểm tra tài liệu chính thức về đối tượng UploadedFile
, bạn có thể sử dụng thuộc tính UploadedFile.temporary_file_path
, nhưng hãy cẩn thận rằng chỉ các tệp được tải lên để đĩa hiển thị nó [nghĩa là thông thường, khi sử dụng trình xử lý tải lên ____10].
upload = request.FILES['ufile']
path = upload.temporary_file_path
Tuy nhiên, trong trường hợp bình thường, bạn muốn sử dụng trực tiếp trình xử lý tệp:
upload = request.FILES['ufile']
content = upload.read[] # For small files
# ... or ...
for chunk in upload.chunks[]:
do_somthing_with_chunk[chunk] # For bigger files
Đã trả lời ngày 15 tháng 7 năm 2011 lúc 6:21Jul 15, 2011 at 6:21
Bạn nên sử dụng
upload = request.FILES['ufile']
path = upload.temporary_file_path
1Bạn sẽ nhận được như thế này
upload = request.FILES['ufile']
path = upload.temporary_file_path
2Và sử dụng
upload = request.FILES['ufile']
path = upload.temporary_file_path
3, tệp tải lên của bạn phải lớn hơn 2,5m.Nếu bạn muốn thay đổi điều này, hãy xem cài đặt tải lên tệp
Đã trả lời ngày 10 tháng 5 năm 2017 lúc 11:32May 10, 2017 at 11:32
暮零天 晨暮零天暮零天晨
611 Huy hiệu bạc7 Huy hiệu đồng1 silver badge7 bronze badges
Chúng tôi không thể nhận được đường dẫn tệp từ yêu cầu bài đăng, chỉ tên tệp, vì Flask không có quyền truy cập hệ thống tệp. Nếu bạn cần lấy tệp và thực hiện một số hoạt động trên đó thì bạn có thể thử tạo thư mục TEMP Lưu tệp ở đó, bạn cũng có thể nhận đường dẫn.
import tempfile
import shutil
dirpath = tempfile.mkdtemp[]
# perform some operations if needed
shutil.rmtree[dirpath] # remove the temp directory
Đã trả lời ngày 25 tháng 5 lúc 16:27May 25 at 16:27
& nbsp; __ file__ không hoạt động trong bối cảnh Notebook Jupyter.__file__ is useful for reading other files and it gives the current location of the running file. It differs in versions. In Python 3.8 and earlier, __file__ returns the path specified when executing the Python command.
Chúng ta có thể nhận được một đường dẫn tương đối nếu một đường dẫn tương đối được chỉ định. Nếu chúng tôi chỉ định một đường dẫn tuyệt đối, một đường dẫn tuyệt đối được trả về. Nhưng trong Python 3.9 trở lên, __file__ luôn trả về một đường dẫn tuyệt đối, mô -đun OS OS cung cấp các tiện ích khác nhau. & NBSP;
Phương pháp 1: Tìm đường dẫn đến tệp đã cho bằng Path.cwd []
Ở đây, ý tưởng về thư mục làm việc hiện tại [CWD] giữ một vị trí quan trọng. Hãy nghĩ về CWD là thư mục mà Python đang chạy. Python giả định rằng tệp bắt đầu trong CWD nếu nó được gọi đơn giản là theo tên, vì vậy một tham chiếu chỉ tên sẽ chỉ hoạt động nếu tệp nằm trong CWD Python. Đường dẫn.cwd [] trả về thư mục hiện tại. & Nbsp;
Python3
upload = request.FILES['ufile']
path = upload.temporary_file_path
4 upload = request.FILES['ufile']
path = upload.temporary_file_path
5upload = request.FILES['ufile']
path = upload.temporary_file_path
6 upload = request.FILES['ufile']
path = upload.temporary_file_path
7upload = request.FILES['ufile']
path = upload.temporary_file_path
8upload = request.FILES['ufile']
path = upload.temporary_file_path
9Output:
C:\Users\int.suraj.gupta
Phương pháp 2: Tìm đường dẫn đến tệp đã cho bằng OS.GetCwd []
Chúng ta có thể có được con đường tuyệt đối của thư mục làm việc hiện tại. Vì vậy, tùy thuộc vào phiên bản được sử dụng, một đường dẫn tương đối hoặc đường dẫn tuyệt đối được truy xuất. Để có được thư mục làm việc hiện tại trong Python, hãy sử dụng phương thức Os.getCwd []. Hàm này của mô -đun HĐH Python trả về chuỗi chứa đường dẫn tuyệt đối đến thư mục làm việc hiện tại.
Python3
upload = request.FILES['ufile']
path = upload.temporary_file_path
6 upload = request.FILES['ufile']
content = upload.read[] # For small files
# ... or ...
for chunk in upload.chunks[]:
do_somthing_with_chunk[chunk] # For bigger files
1upload = request.FILES['ufile']
path = upload.temporary_file_path
8upload = request.FILES['ufile']
content = upload.read[] # For small files
# ... or ...
for chunk in upload.chunks[]:
do_somthing_with_chunk[chunk] # For bigger files
3upload = request.FILES['ufile']
content = upload.read[] # For small files
# ... or ...
for chunk in upload.chunks[]:
do_somthing_with_chunk[chunk] # For bigger files
4upload = request.FILES['ufile']
content = upload.read[] # For small files
# ... or ...
for chunk in upload.chunks[]:
do_somthing_with_chunk[chunk] # For bigger files
5Output:
Get current working directory : C:\Users\int.suraj.gupta
Phương thức 3: Tìm đường dẫn đến tệp đã cho bằng pathlib.path []. Tuyệt đối []Find the path to the given file using pathlib.Path[].absolute[]
Hàm này của mô -đun HĐH Python trả về chuỗi chứa đường dẫn tuyệt đối đến thư mục làm việc hiện tại.
Python3
upload = request.FILES['ufile']
path = upload.temporary_file_path
6 upload = request.FILES['ufile']
content = upload.read[] # For small files
# ... or ...
for chunk in upload.chunks[]:
do_somthing_with_chunk[chunk] # For bigger files
7upload = request.FILES['ufile']
path = upload.temporary_file_path
8upload = request.FILES['ufile']
content = upload.read[] # For small files
# ... or ...
for chunk in upload.chunks[]:
do_somthing_with_chunk[chunk] # For bigger files
9Output:
C:\Users\int.suraj.gupta
Phương pháp 4: Tìm đường dẫn đến tệp đã cho bằng Os.Path.Basenamepath.basename
Chúng ta có thể lấy tên tệp và tên thư mục của tệp đang chạy theo cách dưới đây. Điều quan trọng để hiểu về __file__ là trình thông dịch điều chỉnh nó vào thời gian chạy để Python biết tệp nào đang xử lý khi tập lệnh sử dụng một số mô -đun. Lợi ích của đường dẫn gọi [File__] là nó trả về một chuỗi chứa đường dẫn và tệp bạn hiện đang làm việc. & NBSP;
- Bạn có thể gọi __file__ trong khi sửa đổi một tệp. Kết quả là, nếu bạn cố gắng gọi nó từ trình thông dịch shell, nó sẽ không thực thi.
- & nbsp; __ file__ không hoạt động trong bối cảnh Notebook Jupyter.
Python3
upload = request.FILES['ufile']
path = upload.temporary_file_path
6 upload = request.FILES['ufile']
content = upload.read[] # For small files
# ... or ...
for chunk in upload.chunks[]:
do_somthing_with_chunk[chunk] # For bigger files
1upload = request.FILES['ufile']
path = upload.temporary_file_path
8upload = request.FILES['ufile']
content = upload.read[] # For small files
# ... or ...
for chunk in upload.chunks[]:
do_somthing_with_chunk[chunk] # For bigger files
3import tempfile
import shutil
dirpath = tempfile.mkdtemp[]
# perform some operations if needed
shutil.rmtree[dirpath] # remove the temp directory
4import tempfile
import shutil
dirpath = tempfile.mkdtemp[]
# perform some operations if needed
shutil.rmtree[dirpath] # remove the temp directory
5upload = request.FILES['ufile']
path = upload.temporary_file_path
8upload = request.FILES['ufile']
content = upload.read[] # For small files
# ... or ...
for chunk in upload.chunks[]:
do_somthing_with_chunk[chunk] # For bigger files
3import tempfile
import shutil
dirpath = tempfile.mkdtemp[]
# perform some operations if needed
shutil.rmtree[dirpath] # remove the temp directory
8import tempfile
import shutil
dirpath = tempfile.mkdtemp[]
# perform some operations if needed
shutil.rmtree[dirpath] # remove the temp directory
9Output:
Phương thức 3: Tìm đường dẫn đến tệp đã cho bằng pathlib.path []. Tuyệt đối []
Hàm này của mô -đun HĐH Python trả về chuỗi chứa đường dẫn tuyệt đối đến thư mục làm việc hiện tại.
upload = request.FILES['ufile']
path = upload.temporary_file_path
6 upload = request.FILES['ufile']
content = upload.read[] # For small files
# ... or ...
for chunk in upload.chunks[]:
do_somthing_with_chunk[chunk] # For bigger files
7
upload = request.FILES['ufile']
path = upload.temporary_file_path
upload = request.FILES['ufile']
content = upload.read[] # For small files
# ... or ...
for chunk in upload.chunks[]:
do_somthing_with_chunk[chunk] # For bigger files
Phương pháp 4: Tìm đường dẫn đến tệp đã cho bằng Os.Path.Basename
Python3
upload = request.FILES['ufile']
path = upload.temporary_file_path
6 upload = request.FILES['ufile']
content = upload.read[] # For small files
# ... or ...
for chunk in upload.chunks[]:
do_somthing_with_chunk[chunk] # For bigger files
1upload = request.FILES['ufile']
path = upload.temporary_file_path
8upload = request.FILES['ufile']
content = upload.read[] # For small files
# ... or ...
for chunk in upload.chunks[]:
do_somthing_with_chunk[chunk] # For bigger files
3C:\Users\int.suraj.gupta4
C:\Users\int.suraj.gupta5
C:\Users\int.suraj.gupta6
C:\Users\int.suraj.gupta7
upload = request.FILES['ufile']
path = upload.temporary_file_path
8upload = request.FILES['ufile']
content = upload.read[] # For small files
# ... or ...
for chunk in upload.chunks[]:
do_somthing_with_chunk[chunk] # For bigger files
3Get current working directory : C:\Users\int.suraj.gupta0
C:\Users\int.suraj.gupta5
C:\Users\int.suraj.gupta6
Get current working directory : C:\Users\int.suraj.gupta3
Output:
Example 2:
Phương thức 3: Tìm đường dẫn đến tệp đã cho bằng pathlib.path []. Tuyệt đối [] no error will occur even if we set os.path.abspath[__file__]
Python3
upload = request.FILES['ufile']
path = upload.temporary_file_path
6 upload = request.FILES['ufile']
content = upload.read[] # For small files
# ... or ...
for chunk in upload.chunks[]:
do_somthing_with_chunk[chunk] # For bigger files
1Phương thức 3: Tìm đường dẫn đến tệp đã cho bằng pathlib.path []. Tuyệt đối []
upload = request.FILES['ufile']
path = upload.temporary_file_path
8upload = request.FILES['ufile']
content = upload.read[] # For small files
# ... or ...
for chunk in upload.chunks[]:
do_somthing_with_chunk[chunk] # For bigger files
3C:\Users\int.suraj.gupta1
C:\Users\int.suraj.gupta2
Hàm này của mô -đun HĐH Python trả về chuỗi chứa đường dẫn tuyệt đối đến thư mục làm việc hiện tại.
upload = request.FILES['ufile']
path = upload.temporary_file_path
6 upload = request.FILES['ufile']
content = upload.read[] # For small files
# ... or ...
for chunk in upload.chunks[]:
do_somthing_with_chunk[chunk] # For bigger files
7Phương pháp 4: Tìm đường dẫn đến tệp đã cho bằng Os.Path.Basename
UploadedFile
0
upload = request.FILES['ufile']
path = upload.temporary_file_path
8UploadedFile
2Output: