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!
os.system
ever - to run subprocesses in python, use the subprocess
module. 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.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)