Python3.6 服务器文件操作代码

前提是要引入以下依赖:

import os
import shutil

from django.http import JsonResponse

1 python中存取json和显示中的相关函数:

def get_server_file_total_size(start_path):
    #  函数获取文件中的所有文件的大小
    total_size = 0
    count = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            if not f.startswith('.'):
                count = count + 1
                fp = os.path.join(dirpath, f)
                total_size += os.path.getsize(fp)
    return total_size, count

2 创建目录

def dir_create(request):
    path = request.query_params.get("path", False)
    name = request.query_params.get("name", False)
    path_type = request.query_params.get("type", False)

    dir1 = '/data1/'    #Fixed file directory
    dir2 = '/data2/'    #Fixed file directory

    if path_type is False:
        path = dir1 + path
    else:
        path = dir2 + path

    if os.path.isdir(path):
        if not os.path.exists(path + name):
            os.makedirs(path + name)
            results = {"message": "ok"}
        else:
            results = {"message": "Bad request !"}
    else:
        results = {"message": "Bad request !!"}
    return JsonResponse(results, safe=False)

3 文件及文件夹重命名

def file_rename(request):
    path = request.query_params.get("path", False)
    name = request.query_params.get("name", False)
    new_name = request.query_params.get("newname", False)
    path_type = request.query_params.get("type", False)

    dir1 = '/data1/'   #Fixed file directory
    dir2 = '/data2/'   #Fixed file directory

    if path_type is False:
        dir1 += path
        path = dir1
    else:
        dir2 += path
        path = dir2

    if os.path.isdir(path):
        if os.path.exists(path + name):
            if os.path.exists(path + new_name):
                os.rename(path + name, path + new_name + "_1")
            else:
                os.rename(path + name, path + new_name)
            results = {"message": "ok"}
        else:
            results = {"message": "Bad request !"}
    else:
        results = {"message": "Bad request !!"}
    return JsonResponse(results, safe=False)

4 文件移动(单文件、多文件)

def file_move(request):
    path_src = request.query_params.get("path_src", False)
    path_dst = request.query_params.get("path_dst", False)
    name = request.query_params.get("name", False)
    path_type = request.query_params.get("type", False)

    dir1 = '/data1/'   #Fixed file directory
    dir2 = '/data2/'   #Fixed file directory

    name = json.loads(name)

    if path_type is False:
        path_src = dir1 + path_src
        path_dst = dir1 + path_dst
    else:
        path_src = dir2 + path_src
        path_dst = dir2 + path_dst

    if os.path.isdir(path_src) and os.path.isdir(path_dst):
        for i in range(len(name)):
            if os.path.exists(path_src + name[i]):
                if os.path.exists(path_dst + name[i]):
                    results = {"message": "existed"}
                else:
                    shutil.move(path_src + name[i], path_dst + name[i])
                    results = {"message": "ok"}
            else:
                results = {"message": "Bad request !"}
    else:
        results = {"message": "Bad request !!"}
    return JsonResponse(results, safe=False)

5  文件复制

def file_copy(request):
    path_src = request.query_params.get("path_src", False)
    path_dst = request.query_params.get("path_dst", False)
    name = request.query_params.get("name", False)
    path_type = request.query_params.get("type", False)

    dir1 = '/data1/'   #Fixed file directory
    dir2 = '/data2/'   #Fixed file directory

    name = json.loads(name)

    if path_type is False:
        path_src = dir1 + path_src
        path_dst = dir1 + path_dst
    else:
        path_src = dir2 + path_src
        path_dst = dir2 + path_dst

    if os.path.isdir(path_src) and os.path.isdir(path_dst):
        for i in range(len(name)):
            if os.path.exists(path_src + name[i]):
                if os.path.exists(path_dst + name[i]):
                    results = {"message": "existed"}
                else:
                    if os.path.isfile(path_src + name[i]):
                        path_dst = os.path.join(path_dst, os.path.basename(path_src))
                        shutil.copy(path_src + name[i], path_dst)

                    else:
                        shutil.copytree(path_src + name[i], path_dst + name[i])
                    results = {"message": "ok"}
            else:
                results = {"message": "Bad request !"}
    else:
        results = {"message": "Bad request !!"}
    return JsonResponse(results, safe=False)

6 文件下载

def get_files_by_path(request):
    path = request.query_params.get("path", '')
    name = request.query_params.get("name", False)
    dir = '/data/'   #Fixed file directory
    path = dir + path

    try:
        response = StreamingHttpResponse(open(path + name, 'rb'))
        response['content_type'] = "application/octet-stream"
        response['Content-Length'] = os.path.getsize(path + name)
        response['Content-Disposition'] = "attachment;filename*=utf-8''{}".format(urlquote(name))
        return response
    except Exception:
        raise Http404

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值