malina malina -3 years ago 131
Python Question

moving txt files to from one directory to another using python

I want to move all the text files in one folder to another folder using python. I already looked around and found a piece of code, but I am getting an error

import os, shutil, glob

dst = '/path/to/dir/Caches/\ 4/Blobs '
os.makedirs(/path/to/dir/Tumblr/Uploads) # create destination directory, if needed (similar to mkdir -p)
except OSError:
# The directory already existed, nothing to do pass

for txt_file in glob.iglob('*.txt'):
shutil.copy2(txt_file, dst)

But not getting an error but it is also not moving the files. Also I would want it to move all the files in the Blob folder!
Help is very appreciated!

Answer Source

Try this..

import shutil
import os

source = '/path/to/source_folder'
dest1 = '/path/to/dest_folder'

files = os.listdir(source)

for f in files:
        shutil.move(source+f, dest1)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download