Q: shutil.move() - error message that the destination file does not exist I have the following code to sort CSV files into a directory structure similar to the WAV files the CSV files were created from:
from __future__ import print_function

import os
import shutil

WAV_FILES_PATH = 'g:\\wav_files\\test007\\'
CSV_FILES_PATH = 'g:\\csv_files\\test007\\'

wav_files_path = os.walk(WAV_FILES_PATH)
csv_files_path = os.walk(CSV_FILES_PATH)

# I'm only interested in CSV files in the root for CSV_FILES_PATH
(csv_root, _, csv_files) = csv_files_path.next()

print('Running ...')
for root, subs, files in wav_files_path:
    for file_ in files:
        if file_.endswith('wav'):
            for csv_file in csv_files:
                if(file_.split('.')[0] in csv_file):
                    src = os.path.join(csv_root, csv_file)
                    dst = os.path.join(csv_root, root.replace(WAV_FILES_PATH, ''), csv_file)
                    print('Moving "%s" to "%s" ...' % (src, dst))
                    shutil.move(src, dst)

There are subfolders in WAV_FILES_PATH that contains the WAV files, e.g.
g:\wav_files\test007\run001\
g:\wav_files\test007\run002\

As the CSV files are located unordered in g:\csv_files\test007, I want to clone the directory structure and move the CSV files to their correct folder. In the end, I want to have e.g. g:\csv_files\test007\run001\ to contain the CSV corresponding to the WAV files ing:\wav_files\test007\run001\.
The problem is that the shutil.move() command gives me an IOError [Errnor 2] complaing that the DESTINATION does not exist. This confuses me, as I have write access to the destination and shutil.move() claims that the target directories don't have to exist.
Am I missing something here?
The print() functions prints out the src and dst correctly.
This is the error output:
[...]
C:\Python27\lib\shutil.pyc in copyfile(src, dst)
     80                 raise SpecialFileError("`%s` is a named pipe" % fn)
     81 
     82     with open(src, 'rb') as fsrc:
---> 83         with open(dst, 'wb') as fdst:
     84             copyfileobj(fsrc, fdst)

IOError: [Errno 2] No such file or directory: 'g:\\csv_files\\test007\\run001\\recording_at_20140920_083721.csv'

INFO: I added the error throwing part (the with blocks) directly to my code and it is NOT throwing an error. Now I'm copying the files myself and deleting them thereafter.
It appears to be a bug in how shutil.move() operates. 

A: I have slightly modified your code and I think its generating the expected results.
#dst = os.path.join(csv_root, root.replace(WAV_FILES_PATH, ''), csv_file)
dst = os.path.join(csv_root, root.replace(WAV_FILES_PATH, '')) # Modified

I also added the following logic enter code herebefore executing shutil.move()
if not os.path.exists(dst):
    os.makedirs(dst)

Hope it will also work for you !!!
