[Python] Moving files using OS and Shutil - Python

Discussão em 'Python' iniciado por Stack, Julho 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to move a file based on a keyword in a pdf using Python. What I have so far is this. PyCharm tells me there are no errors, but I'm obviously missing something since it isn't working. It's probably something simple but I'm extremely new to coding in general. Most of this I've pieced together based on stuff I've seen on here.

    import os

    # import PyPDF2
    import PyPDF2

    #import Shutil
    import shutil

    #import PDF Search
    #Set list of client's to search for
    client_names = ["Name1", "Name2", "Name3", "Name4"]

    #Set Scanned Folder
    scanned_path = r'path'


    #Create a list of files in folder
    scanned_file_list = [os.path.join(scanned_path, f) for f in os.listdir(scanned_path)]
    print(scanned_file_list)

    #Loop for file searching
    for document in scanned_file_list:
    lst = []
    if document[-3] == 'pdf':
    file = open(document, "rb")
    reader=PyPDF2.PdfFileReader(file)
    pageObj = reader.getPage(0)
    text = (pageObj.extractText())
    text = text.split(",")
    source_path = document
    for sentence in text:
    lst = []
    for name in client_names:
    if name in sentence:
    if name not in lst:
    lst.append(name)
    if len(lst) > 1:
    file.close()
    elif len(lst) < 1:
    file.close()
    elif lst[0] == client_names[0]:
    #move file to CA folder
    destination_path = r'C:\ClientFolder1'
    shutil.move(source_path,destination_path)
    elif lst[0] == client_names[1]:
    destination_path = r'C:\ClientFolder2'
    shutil.move(source_path,destination_path)
    elif lst[0] == client_names[2]:
    #move file to KB folder
    destination_path = r'C:\ClientFolder3'
    shutil.move(source_path,destination_path)```

    Continue reading...

Compartilhe esta Página