Question

I have a whole bunch of files in a directory. I'd like a program to loop over the files in the directory and prompt me to enter the name of directory (which are all in the same directory) so that the program moves the file to the specified directory.

I'd like a terminal solution, more specifically, Python way would be instructive for me

Was it helpful?

Solution

Your question is a bit vauge on what you need help with, but here is a template to get you started. Use os and shutil to list directories and move files.

import shutil, os

target = raw_input("Target directory: ")

# Make sure the target dir exists!
assert(os.path.exists(target))

for f in os.listdir('.'):
    b = raw_input("Press y to move file %s to %s: " %(f,target))
    if b.lower() == 'y':
        shutil.move(f, target)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top