Search code examples
pythonmvpython-watchdog

not able to move file python


There have been some similar solutions given before but none seem to work for me.

code:

def watch_dir(self, prefix, dest):
    before = dict([(f, None) for f in os.listdir(self.data_dir)])
    while 1:
        time.sleep(5)
        after = dict([(f, None) for f in os.listdir(self.data_dir)])
        new_files = [f for f in after if not f in before and f.startswith(prefix)]
        before = new_files
        for f in new_files:
            os.system('mv {f} {dest}'.format(f=f, dest=dest))

When I print new_files I get -> ('new_files = ', ['sample.tsv'])

but the mv command gives this error: mv: cannot stat 'sample.tsv': No such file or directory

Can someone help me understand what could be going wrong here?!

Thanks!


Solution

    1. don't use os.system ever - to run subprocesses in python, use the subprocess module.
    2. even using subprocess is too much in this case, as you want to move a file so you can just use shutil.move() to do the same without invoking a separate process.
    3. os.listdir returns only the filenames without the path, so you have to add that yourself to be able to find the file: os.path.join(self.datadir, f)
    4. to watch directories/files efficiently without checking them every 5 seconds, you can use the pyinotify module - it uses a efficient system api to watch the directory and will call your function when it changes, no polling involved.