简体   繁体   中英

Shutil.move python script not executing or moving files

So, I've been trying to write a python script to move .jpg's from one file to another and although the program itself (seemingly) runs to completion as it prints "Task completed", the files do not actually move from one folder to the other. This is the script:

    import shutil
    from os.path import join
    import os

    source = join('C','Users','Francisco','Desktop','Test')
    destination1 = join('C','Users','Francisco','Desktop','Archive1')

    for files in source:
        if files.endswith(".JPG"):
            shutil.move(source,destination1)

    print("Task completed");

I've tried running the script through both Command Prompt and the IDLE editor module, and even have Python 3.x set as a path in my Environment Variables, but nothing seems to work. I've become very frustrated by being unable to move the images from one folder to another and would like to see if you guy could help me figure out what the issue is here, be it a problem with the script itself or with the python software on my computer.

I'm a beginner at scripting with python so any help would be greatly appreciated. Thanks in advance.

You're looping through your source path characters, not through the actual folder, a quick fix:

import os
import shutil

source = os.path.join('C:\\', 'Users', 'Francisco', 'Desktop', 'Test')
destination1 = os.path.join('C:\\', 'Users', 'Francisco', 'Desktop', 'Archive1')

for filename in os.listdir(source):
    if filename.lower().endswith(".jpg"):
        shutil.move(os.path.join(source, filename), destination1)

print("Task completed")

You've got some bugs in your code:

import shutil
from os.path import join
import os

source_dir = join('C:/','Users','Francisco','Desktop','Test')
dest = join('C:/','Users','Francisco','Desktop','Archive1')
files = os.listdir(source_dir)

for source in files:
    if source.endswith(".JPG"):
        shutil.move(source, dest)

print("Task completed");

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