Hướng dẫn how do i upload a file to a directory in python? - làm cách nào để tải tệp lên một thư mục trong python?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọccgi environment. One often comes across various web applications in which the client or the users is required to upload data in the form of a file(eg. image file, an audio file, text file, etc). There are two aspects to uploading a file, as there are two sides to that particular interaction being client-side and the server-side. A form needs to be created to accept the user input in the form of a file.
    An HTML form has different attributes that you can set it to, for instance, which URL is the uploaded data is to be submitted is done through the action attribute. An enctype attribute called multi-part/form-data is required in a HTML form to upload a file. Secondly we will be required to use the input tag of HTML and set it equal to “file”. This adds a upload button in addition to an input button in the form. The below code example demonstrates it well:
     

    Bàn luận

    $ . bin/activate
    
    6
    $ . bin/activate
    
    7
    $ . bin/activate
    
    8

    Trong bài viết này, chúng tôi sẽ xem xét quá trình tải lên tệp trong Python bằng môi trường CGI. Người ta thường gặp các ứng dụng web khác nhau trong đó máy khách hoặc người dùng được yêu cầu tải lên dữ liệu dưới dạng tệp (ví dụ: tệp hình ảnh, tệp âm thanh, tệp văn bản, v.v.). Có hai khía cạnh để tải lên một tệp, vì có hai mặt của tương tác cụ thể đó là phía máy khách và phía máy chủ. Một biểu mẫu cần được tạo để chấp nhận đầu vào của người dùng dưới dạng tệp. Một biểu mẫu HTML có các thuộc tính khác nhau mà bạn có thể đặt nó thành, ví dụ, URL là dữ liệu được tải lên sẽ được gửi được thực hiện thông qua thuộc tính hành động . Một thuộc tính Enctype có tên là Multi-Part/Form-Data được yêu cầu trong biểu mẫu HTML để tải lên tệp. Thứ hai, chúng tôi sẽ được yêu cầu sử dụng thẻ đầu vào của HTML và đặt nó bằng với tệp tệp. Điều này thêm một nút tải lên ngoài một nút đầu vào trong biểu mẫu. Ví dụ mã dưới đây thể hiện nó tốt: & nbsp;

    HTML

    $ . bin/activate
    
    6
    $ pip install requests
    
    0
    $ . bin/activate
    
    8

    $ pip install requests
    
    2
    $ . bin/activate
    
    6
    $ pip install requests
    
    4
    $ pip install requests
    
    5
    $ pip install requests
    
    6
    $ pip install requests
    
    7
    $ pip install requests
    
    8

    test_response = requests.post(test_url, files = {"form_field_name": test_file})
    
    1
    $ pip install requests
    
    4
    $ . bin/activate
    
    8

    test_response = requests.post(test_url, files = {"form_field_name": test_file})
    
    1
    $ pip install requests
    
    0
    $ . bin/activate
    
    8

    test_response = requests.post(test_url, files = {"form_field_name": test_file})
    
    1
    $ . bin/activate
    
    7
    $ . bin/activate
    
    8

    $ . bin/activate
    
    6
    import requests
    
    6
    import requests
    
    7
    import requests
    
    8
    import requests
    
    9
    $ pip install requests
    
    6
    test_file = open("my_file.txt", "rb")
    
    1
    test_file = open("my_file.txt", "rb")
    
    2
     

    Hướng dẫn how do i upload a file to a directory in python? - làm cách nào để tải tệp lên một thư mục trong python?

    $ . bin/activate
    
    6
    import requests
    
    6
    test_url = "http://httpbin.org/post"
    
    0
    import requests
    
    8
    import requests
    
    9
    $ pip install requests
    
    6
    test_url = "http://httpbin.org/post"
    
    4
    test_url = "http://httpbin.org/post"
    
    5
    $ pip install requests
    
    6__action has a python script that gets executed when a file is uploaded by the user. On the server end as the python script accepts the uploaded data the field storage object retrieves the submitted name of the file from the form’s “filename”. Now all the server needs to do it is read the file that has been uploaded and write it to the “fileitem”(say, ). At the end of this entire process the uploaded file now will be written to the server.
    So, the python script looks somewhat like the below code: 
     

    Python3

    Đầu ra cho mã HTML ở trên sẽ trông giống như bên dưới: & nbsp; & nbsp;

    Trong mã trên, hành động thuộc tính có tập lệnh Python được thực thi khi một tệp được người dùng tải lên. Trên đầu máy chủ khi tập lệnh Python chấp nhận dữ liệu được tải lên, đối tượng lưu trữ trường lấy lại tên đã gửi của tệp từ biểu mẫu Tên tệp FileName. Bây giờ tất cả các máy chủ cần phải làm nó là đọc tệp đã được tải lên và viết nó vào tệp File FileTem (nói,). Vào cuối toàn bộ quá trình này, tệp được tải lên bây giờ sẽ được ghi vào máy chủ. Vì vậy, tập lệnh Python trông có vẻ giống như mã bên dưới: & nbsp; & nbsp;

    if test_response.ok:
        print("Upload completed successfully!")
        print(test_response.text)
    else:
        print("Something went wrong!")
    
    0
    if test_response.ok:
        print("Upload completed successfully!")
        print(test_response.text)
    else:
        print("Something went wrong!")
    
    1

    if test_response.ok:
        print("Upload completed successfully!")
        print(test_response.text)
    else:
        print("Something went wrong!")
    
    2
    if test_response.ok:
        print("Upload completed successfully!")
        print(test_response.text)
    else:
        print("Something went wrong!")
    
    3
    if test_response.ok:
        print("Upload completed successfully!")
        print(test_response.text)
    else:
        print("Something went wrong!")
    
    4
    if test_response.ok:
        print("Upload completed successfully!")
        print(test_response.text)
    else:
        print("Something went wrong!")
    
    5
    if test_response.ok:
        print("Upload completed successfully!")
        print(test_response.text)
    else:
        print("Something went wrong!")
    
    6

    if test_response.ok:
        print("Upload completed successfully!")
        print(test_response.text)
    else:
        print("Something went wrong!")
    
    9
    python single_uploader.py
    
    4
    python single_uploader.py
    
    5
    python single_uploader.py
    
    6
    python single_uploader.py
    
    7
    python single_uploader.py
    
    8
    python single_uploader.py
    
    9

    if test_response.ok:
        print("Upload completed successfully!")
        print(test_response.text)
    else:
        print("Something went wrong!")
    
    7
    if test_response.ok:
        print("Upload completed successfully!")
        print(test_response.text)
    else:
        print("Something went wrong!")
    
    8
    The above python script doesn’t work on every server as every server has its own dependencies to allow running a script in their server for security reasons. For instance if using an Azure server one would need to import msvcrt which is Microsoft visual C++ runtime module to work.
     


    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

    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    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 quét 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 xét cách tải lên các tệp bằng thư viện

    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    0 của Python. Bài viết sẽ bắt đầu bằng cách bao gồm thư viện
    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    0 và chữ ký chức năng
    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    3. Tiếp theo, chúng tôi sẽ bao gồm cách tải lên một tệp duy nhất bằng gói
    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    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à

    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    5,
    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    6 và
    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    7.

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

    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    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

    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    0 với
    $ . bin/activate
    
    00:

    $ pip install requests
    

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

    $ . bin/activate
    
    01 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
    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    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

    $ . bin/activate
    
    03.

    Hàm

    $ . bin/activate
    
    03 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

    $ . bin/activate
    
    05. Đố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

    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    0 thường xác định tiêu đề
    $ . bin/activate
    
    07, 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
    $ . bin/activate
    
    07, đ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
    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    0 library typically determines the
    $ . bin/activate
    
    07 header, which is a value in bytes. If the file is not read in bytes mode, the library may get an incorrect value for
    $ . bin/activate
    
    07, 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

    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    3 của thư viện
    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    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à
    $ . bin/activate
    
    11. Chúng tôi cũng sẽ lưu phản hồi trong một biến, viết mã sau:

    test_response = requests.post(test_url, files = {"form_field_name": test_file})
    

    Tài sản

    $ . bin/activate
    
    11 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

    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    3 của bạn có thành công hay 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
    $ . bin/activate
    
    14 của đối tượng phản hồi,
    $ . bin/activate
    
    15. 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ó!

    if test_response.ok:
        print("Upload completed successfully!")
        print(test_response.text)
    else:
        print("Something went wrong!")
    

    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

    $ . bin/activate
    
    16:

    python single_uploader.py
    

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

    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    

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

    $ . bin/activate
    
    17 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

    $ . bin/activate
    
    18 và mã thiết lập sau:

    $ . bin/activate
    
    0

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

    $ . bin/activate
    
    19 đó là từ điển có nhiều tên và tệp:

    $ . bin/activate
    
    1

    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:

    $ . bin/activate
    
    2

    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:

    $ . bin/activate
    
    3

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

    $ . bin/activate
    
    16:

    $ . bin/activate
    
    4

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

    $ . bin/activate
    
    5

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

    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    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

    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    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
    Upload completed successfully!
    {
      "args": {}, 
      "data": "", 
      "files": {
        "form_field_name": "This is my file\nI like my file\n"
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "189", 
        "Content-Type": "multipart/form-data; boundary=53bb41eb09d784cedc62d521121269f8", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.25.0", 
        "X-Amzn-Trace-Id": "Root=1-5fc3c190-5dea2c7633a02bcf5e654c2b"
      }, 
      "json": null, 
      "origin": "102.5.105.200", 
      "url": "http://httpbin.org/post"
    }
    
    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.

    Làm cách nào để tải tệp lên một thư mục?

    Trên máy tính của bạn, truy cập Drive.google.com. Mở hoặc tạo một thư mục. Để tải lên các tệp và thư mục, hãy kéo chúng vào thư mục Google Drive.drag them into the Google Drive folder.

    Làm cách nào để tạo một thư mục từ một tệp văn bản trong Python?

    Tạo một tệp văn bản trống Chúng tôi không phải nhập bất kỳ mô -đun nào để tạo tệp mới.Chúng ta có thể tạo một tệp bằng cách sử dụng hàm tích hợp Open ().Chuyển tên tệp và chế độ truy cập cho hàm Open () để tạo tệp.Chế độ truy cập Chỉ định mục đích mở một tệp.create a file using the built-in function open() . Pass the file name and access mode to the open() function to create a file. Access mode specifies the purpose of opening a file.

    Làm cách nào để lưu một hình ảnh trong một thư mục trong Python?

    Ở đây, chúng ta có thể thấy cách lưu một hình ảnh trong Python ...
    Trong ví dụ này, tôi đã nhập một mô -đun có tên Image từ PIL và đã khai báo một hình ảnh biến và hình ảnh được gán.Mở (r'DOWDLOADS \ 3. JPG ') Đường dẫn và tên của hình ảnh cùng với phần mở rộng ..
    Và tuyên bố một biến khác và hình ảnh được gán.Tiết kiệm (búp bê của người Viking. JPG) ..

    Làm thế nào để bạn tải lên một tệp?

    Tải lên và xem tệp..
    Trên điện thoại Android hoặc máy tính bảng của bạn, hãy mở ứng dụng Google Drive ..
    Nhấn vào Thêm ..
    Nhấn vào tải lên ..
    Tìm và nhấn vào các tệp bạn muốn tải lên ..
    Xem các tệp đã tải lên trong ổ đĩa của tôi cho đến khi bạn di chuyển chúng ..