[Python] Python: IF filename in list

Discussão em 'Python' iniciado por Stack, Janeiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to figure out how to go through a folder and move files that are in a list. I have the script creating a list of file names, so now I need to be able to take the files in a folder and move them if they are in the list.

    import os, shutil
    fileList = []
    for root, dirs, files in os.walk(r'K:\Users\User1\Desktop\Python\New folder'):
    for file in files:
    if file.endswith('.txt'):
    fileList.append(file)
    print fileList
    source = "K:\\Users\\User1\\Desktop\\Python\\New folder"
    destination = "K:\\Users\\User1\\Desktop\\Python\\New folder (2)"
    for root, dirs, files in os.walk(r'K:\Users\User1\Desktop\Python\New folder'):
    for file in files:
    if fname in fileList:
    shutil.move(source, destination)


    I've used other snippets I've found online to get the list created, but I haven't been able to understand how to get the file name as a variable to check if it's in the list. Any help is much appreciated.

    Continue reading...

Compartilhe esta Página