Moving all files from one directory to another using Python
Asked Answered
S

12

81

I want to move all text files from one folder to another folder using Python. I found this code:

import os, shutil, glob

dst = '/path/to/dir/Caches/com.apple.Safari/WebKitCache/Version\ 4/Blobs '
try:
    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)

I would want it to move all the files in the Blob folder. I am not getting an error, but it is also not moving the files.

Sandglass answered 24/1, 2017 at 11:18 Comment(0)
B
151

Try this:

import shutil
import os
    
source_dir = '/path/to/source_folder'
target_dir = '/path/to/dest_folder'
    
file_names = os.listdir(source_dir)
    
for file_name in file_names:
    shutil.move(os.path.join(source_dir, file_name), target_dir)
Braunstein answered 24/1, 2017 at 11:36 Comment(5)
Note: The above will move files recursively from source to destination. Also, in my test, the above code is missing a trailing slash in both source and dest1.Garbe
does this move sub-directories (and/or the files inside them) into the destination folder?Ornithopter
@danielbrandstetter shutil.move(src, dst): Recursively move a file or directory (src) to another location (dst).Favorable
You need '/' at the end of the paths srouce and dest1. Also shutil.move(source+f, dest1) this should be shutil.move(source+f, dest1+f) to be extra secure. Otherwise all of source+f becomes a file with path dest1Evaporation
So in summary, if you want subdirectories moved as well, use: for f in files: shutil.move(os.path.join(source, f), os.path.join(dest1,f))Bovine
S
23

Suprised this doesn't have an answer using pathlib, which was introduced in Python 3.4.

Additionally, shutil was updated in Python 3.6 to accept a pathlib object. More details in this PEP-0519.

Pathlib (moves files, does not copy)

from pathlib import Path

src_path = Path('/tmp').joinpath('files_to_move')
# ^ the same as /tmp/files_to_move
#   OR //tmp//files_to_move if on windows. 

for each_file in src_path.glob('*.*'): # grabs all files
    trg_path = src_path.parent # gets the parent of the folder 
    each_file.rename(trg_path.joinpath(each_file.name)) # moves to parent folder.

Pathlib & shutil to copy files.

from pathlib import Path
import shutil

src_path = Path('/tmp').joinpath('files_to_move')
trg_path = src_path.parent # this would be '/tmp/' from the above example.

for src_file in src_path.glob('*.*'):
    shutil.copy(src_file, trg_path)

Before (both examples)

enter image description here

After (move example)

enter image description here

After (copy example)

enter image description here

Shellishellie answered 5/4, 2020 at 13:13 Comment(4)
Thanks, this was exactly what I was looking for!Intersection
I think you mean here to do Path(src_path).glob('*.*')?Not
Path also implements a method called iterdir, in case you don't want to .glob('*.*') docs.python.org/3/library/pathlib.html#pathlib.Path.iterdirNot
@Not nice catch, feel free to edit next time :) you also have rglob to trawl a directory structure.Shellishellie
M
8

Please, take a look at implementation of the copytree function which:

  • List directory files with:

    names = os.listdir(src)

  • Copy files with:

for name in names:
  srcname = os.path.join(src, name)
  dstname = os.path.join(dst, name)
  copy2(srcname, dstname)

Getting dstname is not necessary, because if destination parameter specifies a directory, the file will be copied into dst using the base filename from srcname.

Replace copy2 by move.

Mcgill answered 24/1, 2017 at 11:44 Comment(0)
M
6

Copying the ".txt" file from one folder to another is very simple and question contains the logic. Only missing part is substituting with right information as below:

import os, shutil, glob

src_fldr = r"Source Folder/Directory path"; ## Edit this

dst_fldr = "Destiantion Folder/Directory path"; ## Edit this

try:
  os.makedirs(dst_fldr); ## it creates the destination folder
except:
  print "Folder already exist or some error";

below lines of code will copy the file with *.txt extension files from src_fldr to dst_fldr

for txt_file in glob.glob(src_fldr+"\\*.txt"):
    shutil.copy2(txt_file, dst_fldr);
Molli answered 24/1, 2017 at 12:0 Comment(0)
F
4

This should do the trick. Also read the documentation of the shutil module to choose the function that fits your needs (shutil.copy(), shutil.copy2(), shutil.copyfile() or shutil.move()).

import glob, os, shutil

source_dir = '/path/to/dir/with/files' #Path where your files are at the moment
dst = '/path/to/dir/for/new/files' #Path you want to move your files to
files = glob.iglob(os.path.join(source_dir, "*.txt"))
for file in files:
    if os.path.isfile(file):
        shutil.copy2(file, dst)
Faydra answered 24/1, 2017 at 11:29 Comment(1)
but where do i define the new destination for the txt files?Sandglass
C
4
import shutil 
import os 
import logging

source = '/var/spools/asterisk/monitor' 
dest1 = '/tmp/'


files = os.listdir(source)

for f in files:
        shutil.move(source+f, dest1)

logging.basicConfig(filename='app.log', filemode='w', format='%(name)s
- %(levelname)s - %(message)s')

logging.info('directories moved')

A little bit cooked code with log feature. You can also configure this to run at some period of time using crontab.

* */1 * * * python /home/yourprogram.py > /dev/null 2>&1

runs every hour! cheers

Citarella answered 17/1, 2020 at 12:25 Comment(0)
B
2

Try this:

 if os.path.exists(source_dir):
    try:
        file_names = os.listdir(source_dir)
        if not os.path.exists(target_dir):
            os.makedirs(target_dir)
        for file_name in file_names:
            shutil.move(os.path.join(source_dir, file_name), target_dir)
    except OSError as e:
        print("Error: %s - %s." % (e.filename, e.strerror))
else:
    log.debug(" Directory not exist {}".format(source_dir))
Brodie answered 5/3, 2021 at 8:33 Comment(0)
W
1

Move files with filter( using Path, os,shutil modules):

from pathlib import Path
import shutil
import os

src_path ='/media/shakil/New Volume/python/src'
trg_path ='/media/shakil/New Volume/python/trg'

for src_file in Path(src_path).glob('*.txt*'):
    shutil.move(os.path.join(src_path,src_file),trg_path)
Woodrowwoodruff answered 24/11, 2020 at 1:59 Comment(0)
J
0

For example, if I wanted to move all .txt files from one location to another ( on a Windows OS for instance ) I would do it something like this:

import shutil
import os,glob

inpath = 'R:/demo/in' 
outpath = 'R:/demo/out'

os.chdir(inpath)
for file in glob.glob("*.txt"):

    shutil.move(inpath+'/'+file,outpath)
Johanson answered 4/7, 2020 at 19:45 Comment(0)
F
0
def copy_myfile_dirOne_to_dirSec(src, dest, ext): 

    if not os.path.exists(dest):    # if dest dir is not there then we create here
        os.makedirs(dest);
        
    for item in os.listdir(src):
        if item.endswith(ext):
            s = os.path.join(src, item);
            fd = open(s, 'r');
            data = fd.read();
            fd.close();
            
            fname = str(item); #just taking file name to make this name file is destination dir     
            
            d = os.path.join(dest, fname);
            fd = open(d, 'w');
            fd.write(data);
            fd.close();
    
    print("Files are copyed successfully")
Freeze answered 14/7, 2020 at 18:31 Comment(1)
Code dumps without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it works better than what the OP provided. See How to Answer.Past
P
0

This Script find Docx Files in Directory and create Folder For Docx Files and Move Them into created Directory

import os
import glob
import shutil

docfiles=glob.glob("./**/*.docx" , recursive=True)
if docfiles:
    docdir = os.path.join("./DOCX")
    os.makedirs(docdir , exist_ok = True)
    for docfile in docfiles:
        if docfile in docdir:
            pass
        else:
            shutil.move(os.path.join(docfile),docdir)
            print("Files Moved")
Phrase answered 1/6, 2022 at 8:40 Comment(0)
C
-1

If you prefer to use linux command, os.system() is a great option.

import os
old_dir = 'path1/'
new_dir = 'path2/'
os.system(f'mv {old_dir} {new_dir}')
Clasping answered 7/7, 2023 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.