Search code examples
pythonreadfilewritefile

Moving files to multiple destination


I am trying to move files in path folder to multiple destination directory. So my condition is 70 percent of files to move to dest1 and 30 percent to dest2. What i tried so far gives me some error. I am not sure if the logic is wrong or how to do this. Please post your solution or ideas. Thanks

Code:

import os
import shutil
import random
from shutil import copyfile
path="/Users/kj/Downloads/spam_classifier-master2/data2/data"
dest1="/Users/kj/Downloads/test"
dest2="/Users/kj/Downloads/train"


files=os.listdir(path)
for f in files:
    if (len(f) >0.7 ):
        shutil.move(f,dest2)
    elif (len(f)<0.3):
        shutil.move(f,dest1)

Error:

Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 544, in move
    os.rename(src, real_dst)
FileNotFoundError: [Errno 2] No such file or directory: 'mail0.txt' -> '/Users/kj/Downloads/train'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/kj/Downloads/ef.py", line 13, in <module>
    shutil.move(f,dest2)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 558, in move
    copy_function(src, real_dst)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 257, in copy2
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 120, in copyfile
    with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'mail0.txt'

Solution

  • os.listdir returns just the file names under given path, so when you perform an action on the file name, you should join the file name with the path to obtain the full path first. Also you should divide the file number by the length of the file list to obtain a proper ratio:

    files=os.listdir(path)
    for i, f in enumerate(files):
        if (i + 1) / len(files) > 0.7:
            shutil.move(os.path.join(path, f),dest2)
        else:
            shutil.move(os.path.join(path, f),dest1)