获取文件大小,文件上传速率等代码
时间: 2024-03-02 07:05:27 浏览: 138
获取文件大小的代码:
```python
import os
def get_file_size(file_path):
"""获取文件大小,单位为字节"""
return os.path.getsize(file_path)
```
获取上传速率的代码:
```python
import time
def calculate_upload_speed(file_size, start_time, end_time):
"""计算上传速率,单位为字节/秒"""
time_elapsed = end_time - start_time
return file_size / time_elapsed
```
上传文件的代码:
```python
import requests
def upload_file(file_path, url):
"""上传文件"""
with open(file_path, 'rb') as f:
file_data = f.read()
start_time = time.time()
response = requests.post(url, data=file_data)
end_time = time.time()
if response.status_code == 200:
file_size = get_file_size(file_path)
upload_speed = calculate_upload_speed(file_size, start_time, end_time)
print(f"上传成功!文件大小为{file_size}字节,上传速率为{upload_speed}字节/秒")
else:
print("上传失败!")
```
阅读全文