Hướng dẫn how do you upload multiple files in python? - làm cách nào để bạn tải lên nhiều tệp trong python?

Tôi hơi bối rối, nhưng trực tiếp mở tệp trong yêu cầu (tuy nhiên tương tự được viết trong hướng dẫn yêu cầu chính thức) không phải là "an toàn".

Thử xem:

import os
import requests
file_path = "/home/user_folder/somefile.txt"
files = {'somefile': open(file_path, 'rb')}
r = requests.post('http://httpbin.org/post', files=files)

Vâng, tất cả sẽ ổn, nhưng:

os.rename(file_path, file_path)

Và bạn sẽ nhận được:

PermissionError:The process cannot access the file because it is being used by another process

Xin vui lòng, hãy sửa cho tôi nếu tôi không đúng, nhưng có vẻ như tệp vẫn được mở và tôi không biết cách nào để đóng nó.

Thay vì điều này tôi sử dụng:

import os
import requests
#let it be folder with files to upload
folder = "/home/user_folder/"
#dict for files
upload_list = []
for files in os.listdir(folder):
    with open("{folder}{name}".format(folder=folder, name=files), "rb") as data:
        upload_list.append(files, data.read())
r = request.post("https://httpbin.org/post", files=upload_list)
#trying to rename uploaded files now
for files in os.listdir(folder):
    os.rename("{folder}{name}".format(folder=folder, name=files), "{folder}{name}".format(folder=folder, name=files))

Bây giờ chúng tôi không gặp lỗi, vì vậy tôi khuyên bạn nên sử dụng cách này để tải lên nhiều tệp hoặc bạn có thể nhận được một số lỗi. Hy vọng câu trả lời này tốt giúp ai đó và tiết kiệm thời gian vô giá.

Giới thiệu

Python được hỗ trợ bởi nhiều thư viện giúp đơn giản hóa việc truyền dữ liệu qua HTTP. Thư viện

PermissionError:The process cannot access the file because it is being used by another process
0 là một trong những gói Python phổ biến nhất vì nó được sử dụng rất nhiều trong việc cạo web. Nó cũng phổ biến để tương tác với máy chủ! Thư viện giúp dễ dàng tải lên dữ liệu theo định dạng phổ biến như JSON, nhưng cũng giúp bạn dễ dàng tải lên các tệp.

Trong hướng dẫn này, chúng tôi sẽ xem cách tải lên các tệp bằng thư viện

PermissionError:The process cannot access the file because it is being used by another process
0 của Python. Bài viết sẽ bắt đầu bằng cách bao gồm thư viện
PermissionError:The process cannot access the file because it is being used by another process
0 và chữ ký chức năng
PermissionError:The process cannot access the file because it is being used by another process
3. Tiếp theo, chúng tôi sẽ bao gồm cách tải lên một tệp bằng gói
PermissionError:The process cannot access the file because it is being used by another process
0. Cuối cùng nhưng không kém phần quan trọng, chúng tôi tải lên nhiều tệp trong một yêu cầu.

Tải lên một tệp duy nhất với thư viện yêu cầu của Python

Hướng dẫn này bao gồm cách gửi các tệp, chúng tôi không quan tâm đến cách chúng được tạo. Để làm theo, tạo ba tệp được gọi là

PermissionError:The process cannot access the file because it is being used by another process
5,
PermissionError:The process cannot access the file because it is being used by another process
6 và
PermissionError:The process cannot access the file because it is being used by another process
7.

Điều đầu tiên chúng tôi cần làm là cài đặt thư viện

PermissionError:The process cannot access the file because it is being used by another process
8 của chúng tôi trong không gian làm việc của chúng tôi. Mặc dù không cần thiết, bạn nên cài đặt thư viện trong môi trường ảo:

$ python3 -m venv .

Kích hoạt môi trường ảo để chúng ta không còn tác động đến việc cài đặt Python toàn cầu:

$ . bin/activate

Bây giờ chúng ta hãy cài đặt thư viện

PermissionError:The process cannot access the file because it is being used by another process
0 với
import os
import requests
#let it be folder with files to upload
folder = "/home/user_folder/"
#dict for files
upload_list = []
for files in os.listdir(folder):
    with open("{folder}{name}".format(folder=folder, name=files), "rb") as data:
        upload_list.append(files, data.read())
r = request.post("https://httpbin.org/post", files=upload_list)
#trying to rename uploaded files now
for files in os.listdir(folder):
    os.rename("{folder}{name}".format(folder=folder, name=files), "{folder}{name}".format(folder=folder, name=files))
0:

$ pip install requests

Tạo một tệp mới có tên

import os
import requests
#let it be folder with files to upload
folder = "/home/user_folder/"
#dict for files
upload_list = []
for files in os.listdir(folder):
    with open("{folder}{name}".format(folder=folder, name=files), "rb") as data:
        upload_list.append(files, data.read())
r = request.post("https://httpbin.org/post", files=upload_list)
#trying to rename uploaded files now
for files in os.listdir(folder):
    os.rename("{folder}{name}".format(folder=folder, name=files), "{folder}{name}".format(folder=folder, name=files))
1 sẽ lưu trữ mã của chúng tôi. Trong tệp đó, hãy bắt đầu bằng cách nhập thư viện
PermissionError:The process cannot access the file because it is being used by another process
0:

import requests

Bây giờ chúng tôi đã thiết lập để tải lên một tập tin! Khi tải lên một tệp, chúng ta cần mở tệp và truyền phát nội dung. Rốt cuộc, chúng tôi không thể tải lên một tệp mà chúng tôi không có quyền truy cập. Chúng tôi sẽ làm điều này với chức năng

import os
import requests
#let it be folder with files to upload
folder = "/home/user_folder/"
#dict for files
upload_list = []
for files in os.listdir(folder):
    with open("{folder}{name}".format(folder=folder, name=files), "rb") as data:
        upload_list.append(files, data.read())
r = request.post("https://httpbin.org/post", files=upload_list)
#trying to rename uploaded files now
for files in os.listdir(folder):
    os.rename("{folder}{name}".format(folder=folder, name=files), "{folder}{name}".format(folder=folder, name=files))
3.

Hàm

import os
import requests
#let it be folder with files to upload
folder = "/home/user_folder/"
#dict for files
upload_list = []
for files in os.listdir(folder):
    with open("{folder}{name}".format(folder=folder, name=files), "rb") as data:
        upload_list.append(files, data.read())
r = request.post("https://httpbin.org/post", files=upload_list)
#trying to rename uploaded files now
for files in os.listdir(folder):
    os.rename("{folder}{name}".format(folder=folder, name=files), "{folder}{name}".format(folder=folder, name=files))
3 chấp nhận hai tham số: đường dẫn của tệp và chế độ. Đường dẫn của tệp có thể là một đường dẫn tuyệt đối hoặc đường dẫn tương đối đến nơi tập lệnh đang được chạy. Nếu bạn đang tải lên một tệp trong cùng một thư mục, bạn chỉ có thể sử dụng tên của tệp.

Đối số thứ hai, chế độ, sẽ lấy giá trị "đọc nhị phân" được biểu thị bằng

import os
import requests
#let it be folder with files to upload
folder = "/home/user_folder/"
#dict for files
upload_list = []
for files in os.listdir(folder):
    with open("{folder}{name}".format(folder=folder, name=files), "rb") as data:
        upload_list.append(files, data.read())
r = request.post("https://httpbin.org/post", files=upload_list)
#trying to rename uploaded files now
for files in os.listdir(folder):
    os.rename("{folder}{name}".format(folder=folder, name=files), "{folder}{name}".format(folder=folder, name=files))
5. Đối số này nói với máy tính rằng chúng tôi muốn mở tệp ở chế độ đọc và chúng tôi muốn tiêu thụ dữ liệu của tệp ở định dạng nhị phân:

test_file = open("my_file.txt", "rb")

Lưu ý: Điều quan trọng là phải đọc tệp ở chế độ nhị phân. Thư viện

PermissionError:The process cannot access the file because it is being used by another process
0 thường xác định tiêu đề
import os
import requests
#let it be folder with files to upload
folder = "/home/user_folder/"
#dict for files
upload_list = []
for files in os.listdir(folder):
    with open("{folder}{name}".format(folder=folder, name=files), "rb") as data:
        upload_list.append(files, data.read())
r = request.post("https://httpbin.org/post", files=upload_list)
#trying to rename uploaded files now
for files in os.listdir(folder):
    os.rename("{folder}{name}".format(folder=folder, name=files), "{folder}{name}".format(folder=folder, name=files))
7, là giá trị trong byte. Nếu tệp không được đọc trong chế độ byte, thư viện có thể nhận được giá trị không chính xác cho
import os
import requests
#let it be folder with files to upload
folder = "/home/user_folder/"
#dict for files
upload_list = []
for files in os.listdir(folder):
    with open("{folder}{name}".format(folder=folder, name=files), "rb") as data:
        upload_list.append(files, data.read())
r = request.post("https://httpbin.org/post", files=upload_list)
#trying to rename uploaded files now
for files in os.listdir(folder):
    os.rename("{folder}{name}".format(folder=folder, name=files), "{folder}{name}".format(folder=folder, name=files))
7, điều này sẽ gây ra lỗi trong quá trình gửi tệp.
: it's important to read the file in binary mode. The
PermissionError:The process cannot access the file because it is being used by another process
0 library typically determines the
import os
import requests
#let it be folder with files to upload
folder = "/home/user_folder/"
#dict for files
upload_list = []
for files in os.listdir(folder):
    with open("{folder}{name}".format(folder=folder, name=files), "rb") as data:
        upload_list.append(files, data.read())
r = request.post("https://httpbin.org/post", files=upload_list)
#trying to rename uploaded files now
for files in os.listdir(folder):
    os.rename("{folder}{name}".format(folder=folder, name=files), "{folder}{name}".format(folder=folder, name=files))
7 header, which is a value in bytes. If the file is not read in bytes mode, the library may get an incorrect value for
import os
import requests
#let it be folder with files to upload
folder = "/home/user_folder/"
#dict for files
upload_list = []
for files in os.listdir(folder):
    with open("{folder}{name}".format(folder=folder, name=files), "rb") as data:
        upload_list.append(files, data.read())
r = request.post("https://httpbin.org/post", files=upload_list)
#trying to rename uploaded files now
for files in os.listdir(folder):
    os.rename("{folder}{name}".format(folder=folder, name=files), "{folder}{name}".format(folder=folder, name=files))
7, which would cause errors during file submission.

Đối với hướng dẫn này, chúng tôi sẽ thực hiện các yêu cầu cho dịch vụ HTTPBIN miễn phí. API này cho phép các nhà phát triển kiểm tra các yêu cầu HTTP của họ. Hãy tạo một biến lưu trữ URL, chúng tôi sẽ đăng các tệp của chúng tôi lên:

test_url = "http://httpbin.org/post"

Bây giờ chúng tôi có mọi thứ để đưa ra yêu cầu. Chúng tôi sẽ sử dụng phương thức

PermissionError:The process cannot access the file because it is being used by another process
3 của thư viện
PermissionError:The process cannot access the file because it is being used by another process
0 để tải lên tệp. Chúng tôi cần hai đối số để thực hiện công việc này: URL của thuộc tính máy chủ và
$ python3 -m venv .
1. Chúng tôi cũng sẽ lưu phản hồi trong một biến, viết mã sau:

os.rename(file_path, file_path)
0

Tài sản

$ python3 -m venv .
1 có một từ điển. Khóa là tên của trường biểu mẫu chấp nhận tệp. Giá trị là byte của tệp đã mở mà bạn muốn tải lên.

Thông thường để kiểm tra xem phương thức

PermissionError:The process cannot access the file because it is being used by another process
3 của bạn có thành công không, chúng tôi kiểm tra mã trạng thái HTTP của phản hồi. Chúng ta có thể sử dụng thuộc tính
$ python3 -m venv .
4 của đối tượng phản hồi,
$ python3 -m venv .
5. Nếu đó là sự thật, chúng tôi sẽ in ra phản hồi từ máy chủ HTTP, trong trường hợp này, nó sẽ lặp lại yêu cầu:

Kiểm tra hướng dẫn thực hành của chúng tôi, thực tế để học Git, với các thực hành tốt nhất, các tiêu chuẩn được công nghiệp chấp nhận và bao gồm bảng gian lận. Ngừng các lệnh git googling và thực sự tìm hiểu nó!

os.rename(file_path, file_path)
1

Hãy thử nó! Trong thiết bị đầu cuối, thực thi tập lệnh của bạn bằng lệnh

$ python3 -m venv .
6:

os.rename(file_path, file_path)
2

Đầu ra của bạn sẽ tương tự như thế này:

os.rename(file_path, file_path)
3

Khi kiểm tra Sanity, bạn có thể xác minh giá trị

$ python3 -m venv .
7 khớp với những gì trong tệp của bạn.

Tải lên nhiều tệp với thư viện yêu cầu của Python

Tải lên nhiều tệp bằng cách sử dụng các yêu cầu khá giống với một tệp duy nhất, với sự khác biệt chính là việc chúng tôi sử dụng danh sách. Tạo một tệp mới có tên

$ python3 -m venv .
8 và mã thiết lập sau:

os.rename(file_path, file_path)
4

Bây giờ, hãy tạo một biến có tên là

$ python3 -m venv .
9 là từ điển có nhiều tên và tệp:

os.rename(file_path, file_path)
5

Giống như trước đây, các khóa là tên của các trường biểu mẫu và các giá trị là các tệp trong byte.

Chúng tôi cũng có thể tạo các biến tệp của chúng tôi như một danh sách các bộ dữ liệu. Mỗi tuple chứa tên của trường biểu mẫu Chấp nhận tệp, theo sau là nội dung của tệp trong byte:

os.rename(file_path, file_path)
6

Hoặc là hoạt động nên chọn bất cứ thứ gì bạn thích!

Khi danh sách các tệp đã sẵn sàng, bạn có thể gửi yêu cầu và kiểm tra phản hồi của nó như trước:

os.rename(file_path, file_path)
7

Thực hiện tập lệnh này bằng lệnh

$ python3 -m venv .
6:

os.rename(file_path, file_path)
8

Bạn sẽ thấy đầu ra này:

os.rename(file_path, file_path)
9

Làm tốt lắm! Bạn có thể tải lên một và nhiều tệp với

PermissionError:The process cannot access the file because it is being used by another process
0!

Sự kết luận

Trong bài viết này, chúng tôi đã học cách tải lên các tệp trong Python bằng thư viện

PermissionError:The process cannot access the file because it is being used by another process
0. Trong đó đó là một tệp hoặc nhiều tệp, chỉ cần một vài điều chỉnh với phương thức
PermissionError:The process cannot access the file because it is being used by another process
3. Chúng tôi cũng đã xác minh phản hồi của chúng tôi để đảm bảo rằng tải lên của chúng tôi đã thành công.