Hướng dẫn how do i get the full path of an uploaded file in python? - làm cách nào để lấy đường dẫn đầy đủ của tệp đã tải lên trong python?

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ó?

Hướng dẫn how do i get the full path of an uploaded file in python? - làm cách nào để lấy đường dẫn đầy đủ của tệp đã tải lên trong python?

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
1

Bạn sẽ nhận được như thế này

upload = request.FILES['ufile']
path = upload.temporary_file_path
2

Và 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

Hướng dẫn how do i get the full path of an uploaded file in python? - làm cách nào để lấy đường dẫn đầy đủ của tệp đã tải lên trong python?

暮零天 晨暮零天暮零天晨

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

Hướng dẫn how do i get the full path of an uploaded file in python? - làm cách nào để lấy đường dẫn đầy đủ của tệp đã tải lên trong python?

& 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
5
upload = request.FILES['ufile']
path = upload.temporary_file_path
6
upload = request.FILES['ufile']
path = upload.temporary_file_path
7

upload = request.FILES['ufile']
path = upload.temporary_file_path
8
upload = request.FILES['ufile']
path = upload.temporary_file_path
9

Output:

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
1

upload = request.FILES['ufile']
path = upload.temporary_file_path
8
upload = request.FILES['ufile']
content = upload.read()  # For small files
# ... or ...
for chunk in upload.chunks():
    do_somthing_with_chunk(chunk)  # For bigger files
3
upload = request.FILES['ufile']
content = upload.read()  # For small files
# ... or ...
for chunk in upload.chunks():
    do_somthing_with_chunk(chunk)  # For bigger files
4
upload = request.FILES['ufile']
content = upload.read()  # For small files
# ... or ...
for chunk in upload.chunks():
    do_somthing_with_chunk(chunk)  # For bigger files
5

Output:

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
7

upload = request.FILES['ufile']
path = upload.temporary_file_path
8
upload = request.FILES['ufile']
content = upload.read()  # For small files
# ... or ...
for chunk in upload.chunks():
    do_somthing_with_chunk(chunk)  # For bigger files
9

Output:

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
1

upload = request.FILES['ufile']
path = upload.temporary_file_path
8
upload = request.FILES['ufile']
content = upload.read()  # For small files
# ... or ...
for chunk in upload.chunks():
    do_somthing_with_chunk(chunk)  # For bigger files
3
import tempfile
import shutil

dirpath = tempfile.mkdtemp()
# perform some operations if needed
shutil.rmtree(dirpath) # remove the  temp directory
4
import tempfile
import shutil

dirpath = tempfile.mkdtemp()
# perform some operations if needed
shutil.rmtree(dirpath) # remove the  temp directory
5

upload = request.FILES['ufile']
path = upload.temporary_file_path
8
upload = request.FILES['ufile']
content = upload.read()  # For small files
# ... or ...
for chunk in upload.chunks():
    do_somthing_with_chunk(chunk)  # For bigger files
3
import tempfile
import shutil

dirpath = tempfile.mkdtemp()
# perform some operations if needed
shutil.rmtree(dirpath) # remove the  temp directory
8
import tempfile
import shutil

dirpath = tempfile.mkdtemp()
# perform some operations if needed
shutil.rmtree(dirpath) # remove the  temp directory
9

Output:

Hướng dẫn how do i get the full path of an uploaded file in python? - làm cách nào để lấy đường dẫn đầy đủ của tệp đã tải lên trong python?

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

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
1

upload = request.FILES['ufile']
path = upload.temporary_file_path
8
upload = request.FILES['ufile']
content = upload.read()  # For small files
# ... or ...
for chunk in upload.chunks():
    do_somthing_with_chunk(chunk)  # For bigger files
3
C:\Users\int.suraj.gupta
4
C:\Users\int.suraj.gupta
5

C:\Users\int.suraj.gupta
6
C:\Users\int.suraj.gupta
7

upload = request.FILES['ufile']
path = upload.temporary_file_path
8
upload = request.FILES['ufile']
content = upload.read()  # For small files
# ... or ...
for chunk in upload.chunks():
    do_somthing_with_chunk(chunk)  # For bigger files
3
Get current working directory :  C:\Users\int.suraj.gupta
0
C:\Users\int.suraj.gupta
5

C:\Users\int.suraj.gupta
6
Get current working directory :  C:\Users\int.suraj.gupta
3

Output:

Hướng dẫn how do i get the full path of an uploaded file in python? - làm cách nào để lấy đường dẫn đầy đủ của tệp đã tải lên trong python?

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
1

Phươ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
8
upload = request.FILES['ufile']
content = upload.read()  # For small files
# ... or ...
for chunk in upload.chunks():
    do_somthing_with_chunk(chunk)  # For bigger files
3
C:\Users\int.suraj.gupta
1
C:\Users\int.suraj.gupta
2

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

Phương pháp 4: Tìm đường dẫn đến tệp đã cho bằng Os.Path.Basename

UploadedFile0

upload = request.FILES['ufile']
path = upload.temporary_file_path
8UploadedFile2

Output:

Hướng dẫn how do i get the full path of an uploaded file in python? - làm cách nào để lấy đường dẫn đầy đủ của tệp đã tải lên trong python?


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.__file__. __file__ is useful for reading other files and it gives the current location of the running file.