Create directory on boot using django

After downloading the file from the user interface, how to create a new directory with the current time stamp in / opt / files / and copy the downloaded zip file to this directory and unzip the zip file to a new directory and save the new directory name in a variable

def upload_info(request):
    if request.method == 'POST':
        file=request.FILES['file']
        dir = "/opt/files"
        file_name = "%s/%s" % (dir, file.name)
        form = UploadFileForm(request.POST, request.FILES)
        try:
            handle_uploaded_file( file_name , file )

def handle_uploaded_file(file_name,f):
    destination = open(file_name, 'wb+')
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()
    return
+3
source share
2 answers

Directory creation can be achieved using the Python module os(see documentation ). For instance:

import os
from datetime import datetime
dirname = datetime.now().strftime('%Y.%m.%d.%H.%M.%S') #2010.08.09.12.08.45 
os.mkdir(os.path.join('/opt/files', dirname))

os.rename (documentation), ( , ). Python module ( gzip ).

+8

:

def makedirs(path):
    try:
        os.makedirs(path)
    except OSError as e:
        if e.errno == 17:
            # Dir already exists. No biggie.
            pass
0

Source: https://habr.com/ru/post/1758674/


All Articles