user1530081
user1530081

Reputation: 1

Unable to move mp4 files to another directory in ubuntu using python

I am having difficulty moving mp4 files from one directory to another (Ubuntu Linux). The code i have included herewith seems to work perfectly when I move .py files between directory's. I have done some research on Google searching for an answer but to no avail. i have found answers pointing to permissions etc and i have found help from the following urls.

http://stackoverflow.com/questions/13193015/shutil-move-ioerror-errno-2-when-in-loop

http://stackoverflow.com/questions/7432197/python-recursive-find-files-and-move-to-one-destination-directory

I am new to python and just learning. please can you assist with the code i have included and with the error message i get when i run my python script to move .mp4 files.

sudo python defmove.py /home/iain/dwhelper /home/iain/newfolder .mp4

(i am running the script from the directory where the defmove.py script resides and i have also made sure that newfolder does not exist prior to running defmove.py)

import os
import sys
import shutil

def movefiles(src,dest,ext):
    if not os.path.isdir(dest):
        os.mkdir(dest)
            for root,dirs,files in os.walk(src):
                for f in files:
                    if f.endswith(ext):
                         shutil.move(f,dest)

def main():
    if len(sys.argv) != 4:
        print 'incorrect number of paramaters'
        sys.exit(1)
    else:
         src  = sys.argv[1]
         dest = sys.argv[2]
         ext  = sys.argv[3]
         movefiles(src,dest,ext)

main()

Traceback (most recent call last):
  File "defmove.py", line 24, in <modeule>
   main()
  File "defmove.py", line 22, in main
    movefiles(src,dest,ext)
  File "defmove.py", line 11, in movefiles
    shutil.move(f,dest)
  File "/usr/lib/python2.7/shutil.py", line 301, in move
    copy2(src, real_dst)
  File "/usr/lib/python2.7/shutil.py", line 130, in copy2
    copyfile(src,dest)
  File "/usr/lib/python2.7/shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 2] No suck file or directory: 'feelslike.mp4'

Upvotes: 0

Views: 905

Answers (1)

wflynny
wflynny

Reputation: 18521

When python I/O is given a filename, it assumes that the file is in the current directory, or somewhere on it's path; if it's not in any of those places, it yields an IOError. Therefore, when you are accessing files in directories other than your current directory, it's important to specific the path to that file.

In your code, calling shutils.move with f is just supplying the function a file name---the path to that filename has been stripped off. Therefore, your call to shutils.move should look like

shutil.move(os.path.join(root, f), dest)

Upvotes: 1

Related Questions