Sampath Shanbhag
Sampath Shanbhag

Reputation: 127

Need to store filenames in a list

I am trying to create unique folder once i receive files from server for further processing.

When I upload the files directly onto the server it is possible to create unique folder and place files, but when i upload folder containing files it is not able to create unique folder. Can someone help me with this?

I have uploaded multiple files to the server and created unique folders.

But when I pass folder to server I am not able to create unique folders.

def create_unique_folder(root_dir):
    folder_name = str(uuid.uuid4())
    dir_path = root_dir + '/' + folder_name
    try:
        os.mkdir(dir_path)
    except Exception as e:
        print(e.args)
        return None
    return folder_name

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        resp = {}
        files = request.files.getlist('files')
        print(files)
        #print(type(files))
        folder_name = UPLOAD_FOLDER +  create_unique_folder(app.config['UPLOAD_FOLDER'])
        if folder_name != None:
            for f in files:
                print(folder_name)
                file_path = folder_name + '/' + f.filename
                f.save(os.path.join(folder_name, f.filename))
                file = f
                print('inside upload file')
                print('file')
                filename = f.filename
                ffilename = secure_filename(file.filename)
                print(filename)

The above code creates unique folder as the files variable will have

files = [, , ]

But when I upload a folder my files variable will be files2 = [, , ]

I think because of folder name coming in along with the filename it is not able to create unique folder.

Can someone please tell me how can I get File2 in files format?

Upvotes: 0

Views: 39

Answers (1)

Armali
Armali

Reputation: 19385

I think because of folder name coming in along with the filename it is not able to create unique folder.

Your description is a bit misleading - the above code does not fail to create unique folder, but rather to create subfolders (like resumes/) within it. In order to do that, you could from pathlib import Path and insert

                Path(file_path).parent.mkdir(parents=True, exist_ok=True)

before the line f.save(…).

Upvotes: 1

Related Questions