帮我写一段python代码,实现如下功能:1.文件上传 2.文件下载 3.文件属性输出 4.文件base64到原文件类型互转。

动态 未结 0 77
小小草
小小草 LV1 2023年9月23日 23:40 发表
好的,以下是实现以上功能的Python代码: ```python import os import base64 # 文件上传函数 def upload_file(file_path, server_path): with open(file_path, 'rb') as f: file_content = f.read() with open(server_path, 'wb') as f: f.write(file_content) # 文件下载函数 def download_file(server_path, file_path): with open(server_path, 'rb') as f: file_content = f.read() with open(file_path, 'wb') as f: f.write(file_content) # 文件属性输出函数 def print_file_info(file_path): file_size = os.path.getsize(file_path) file_create_time = os.path.getctime(file_path) file_modify_time = os.path.getmtime(file_path) print(f"文件大小:{file_size}字节") print(f"创建时间:{file_create_time}") print(f"修改时间:{file_modify_time}") # 文件base64到原文件类型互转函数 def base64_to_file(base64_str, file_path): with open(file_path, 'wb') as f: f.write(base64.b64decode(base64_str)) def file_to_base64(file_path): with open(file_path, 'rb') as f: file_content = f.read() return base64.b64encode(file_content).decode('utf-8') ``` 使用示例: ```python # 上传文件 upload_file('test.txt', 'server/test.txt') # 下载文件 download_file('server/test.txt', 'test_download.txt') # 输出文件属性 print_file_info('test.txt') # 文件base64到原文件类型互转 base64_str = file_to_base64('test.txt') print(base64_str) base64_to_file(base64_str, 'test_base64.txt') ```
收藏(0)  分享
相关标签: 智能问答
问题没解决?让AI助手帮你作答 AI助手
0个回复
  • 消灭零回复