简体   繁体   中英

Organizing files into folders based on filename

I'm trying to create a program that reads pdf files (eg files.pdf), separates them by name and moves them to their relevant folder (based on name), like this:

Files:
house.pdf
house_blue.pdf
road.pdf

Folders:
house = house.pdf, house_blue.pdf
road  = road.pdf

My code by now:

for f in os.listdir():
    file_name, file_ext = os.path.splitext(f)
    if len(file_name.split('_')) == 2:
        f_name, f_course = file_name.split('_')
    elif len(file_name.split('_')) != 2:
        f_name = file_name

def movefile (file_name):
    new_dir =  src + "/" + file_name
    for file_name in os.listdir():
        if not os.path.exists(new_dir):
            os.mkdir(new_dir)
        if file_name.startswith(file_name):
            shutil.move(src+"/"+file_name, new_dir)

It's working fine, the only problem is that the for statement is resulting in only the last pdf file, creating only one folder and copying all files to that folder.

Can someone help me where it is wrong?

Thanks in advance.

Edit: Code Working:

src = (os.getcwd())

for f in os.walk(src):
    for f in os.listdir(src):
        if os.path.isfile(src): #Checar se possui arquivos dentro da   pasta(src).
            continue
        lfolders = os.listdir(src) #Atribuir na variavel lfolders todos os diretórios na pasta (src).
        file_name, file_ext = os.path.splitext(f) #separar nome da extensão (.pdf)
        if len(file_name.split('_')) == 2: #se possuir dois nomes (nome e curso)
            f_name, f_course = file_name.split('_')
            try:
                if f_name not in lfolders:
                    for dirmake in os.listdir(src):
                        os.makedirs(os.path.join(src, f_name))
                elif f_name in lfolders:
                    shutil.move(f, src+"/"+f_name)
            except OSError:
                pass

        elif len(file_name.split(')')) != 2: #se possuir um nome (nome)
            f_name = file_name
            try:
                if f_name not in lfolders:
                    for dirmake in os.listdir(src):
                        os.makedirs(os.path.join(src, f_name))
                elif f_name in lfolders:
                    shutil.move(f, src+"/"+f_name)
            except OSError:
                pass

If I understand correctly, you need to do something for every iteration of the loop.

for f in os.listdir():
    file_name, file_ext = os.path.splitext(f)
    if file_ext == 'pdf':
        if len(file_name.split('_')) == 2:
            folder, f_course = file_name.split('_')
        elif len(file_name.split('_')) != 2:
            folder = file_name
    print("Ready to move {} into folder {}".format(file_name, folder)) 
    movefile(f, folder)  # Call your function, add a parameter for the folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM