简单分享下Python文件操作

1. 上传文件到服务器

场景描述:

使用 requests 库上传文件到服务器。

import requestsdef test_upload_file():    url = "https://api.example.com/upload"    file_path = "path/to/file.txt"    with open(file_path, "rb") as file:        files = {"file": ("file.txt", file)}        response = requests.post(url, files=files)        assert response.status_code == 200        assert response.json()["message"] == "File uploaded successfully"

输出结果:

PASSED‍

INSPIRATION

2. 下载文件并保存到本地

场景描述:

下载文件并保存到本地。

import requestsdef test_download_file():    url = "https://api.example.com/download"    local_path = "path/to/local/file.txt"    response = requests.get(url)    with open(local_path, "wb") as file:        file.write(response.content)    assert response.status_code == 200

输出结果:

PASSED‍

INSPIRATION

3. 比较两个文件内容

场景描述:

比较服务器返回的文件与本地文件的内容是否一致。

import requestsdef test_compare_files():    url = "https://api.example.com/download"    local_path = "path/to/local/file.txt"    remote_path = "path/to/remote/file.txt"    with open(local_path, "r") as local_file, open(remote_path, "r") as remote_file:        local_content = local_file.read()        remote_content = remote_file.read()    assert local_content == remote_content

输出结果:

PASSED‍

INSPIRATION

4. 读取 CSV 文件并作为请求参数

场景描述:

读取 CSV 文件并将其中的数据作为请求参数发送给 API。

import csvimport requestsdef test_send_csv_data():    url = "https://api.example.com/csv"    file_path = "path/to/file.csv"    with open(file_path, newline='') as csvfile:        reader = csv.DictReader(csvfile)        for row in reader:            response = requests.post(url, data=row)            assert response.status_code == 200

输出结果:

PASSED‍

INSPIRATION

5. 读取 JSON 文件并发送 POST 请求

场景描述:

读取 JSON 文件并将其内容作为请求体发送到 API。

import jsonimport requestsdef test_send_json_data():    url = "https://api.example.com/json"    file_path = "path/to/file.json"    with open(file_path, "r") as file:        data = json.load(file)    response = requests.post(url, json=data)    assert response.status_code == 200

输出结果:

PASSED‍

INSPIRATION

6. 下载文件并验证文件大小

场景描述:

下载文件并验证文件大小。

import requestsdef test_validate_file_size():    url = "https://api.example.com/download"    response = requests.get(url)    file_size = len(response.content)    assert file_size == 1024  # 假设文件大小为 1024 字节

输出结果:

PASSED‍

INSPIRATION

7. 上传文件并验证文件名

场景描述:

上传文件并验证服务器返回的文件名。

​​​​​​​

import requestsdef test_validate_uploaded_filename():    url = "https://api.example.com/upload"    file_path = "path/to/file.txt"    with open(file_path, "rb") as file:        files = {"file": ("file.txt", file)}        response = requests.post(url, files=files)        assert response.status_code == 200        assert response.json()["filename"] == "file.txt"

输出结果:

PASSED‍

INSPIRATION

8. 上传多个文件

场景描述:

同时上传多个文件。

import requestsdef test_upload_multiple_files():    url = "https://api.example.com/upload"    files = [("file", ("file1.txt", open("path/to/file1.txt", "rb"))),             ("file", ("file2.txt", open("path/to/file2.txt", "rb")))]    response = requests.post(url, files=files)    assert response.status_code == 200    assert len(response.json()["uploaded_files"]) == 2

输出结果:

PASSED‍

INSPIRATION

9. 上传文件并验证 MD5 校验码

场景描述:

上传文件并验证服务器返回的 MD5 校验码与计算出的校验码是否一致。

import hashlibimport requestsdef test_validate_md5_checksum():    url = "https://api.example.com/upload"    file_path = "path/to/file.txt"    with open(file_path, "rb") as file:        files = {"file": ("file.txt", file)}        response = requests.post(url, files=files)        assert response.status_code == 200        md5_hash = hashlib.md5()        with open(file_path, "rb") as file:            chunk = file.read(8192)            while chunk:                md5_hash.update(chunk)                chunk = file.read(8192)        assert response.json()["md5"] == md5_hash.hexdigest()

输出结果:

PASSED‍

INSPIRATION

10. 上传文件并验证文件类型

场景描述:

上传文件并验证服务器返回的 MIME 类型是否正确。

import requestsimport mimetypesdef test_validate_mimetype():    url = "https://api.example.com/upload"    file_path = "path/to/image.jpg"    with open(file_path, "rb") as file:        files = {"file": ("image.jpg", file)}        response = requests.post(url, files=files)        assert response.status_code == 200        assert response.json()["mimetype"] == mimetypes.guess_type(file_path)[0]

输出结果:

PASSED‍

INSPIRATION

图片

以上示例中的 URL 和文件路径均为示例性质,你需要根据实际情况替换它们。这些示例涵盖了文件上传、下载、验证文件内容、文件大小、文件名、MD5 校验码、MIME 类型等常见操作。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小软件大世界

谢谢支持,我将会更加努力的寻找

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值