0

I want to move all new added file from a directory (ftp directory) to another directory (Documents). I use vsftpd on my ubuntu 22.04. I've created a simple code to move new file like this

import shutil
import os

source_dir = 'source_dir' # ftp directory
target_dir = 'target_dir'

while True:
  file_names = os.listdir(source_dir)
  for file_name in file_names:
      # this line is executed before the file is completely added.
      shutil.move(os.path.join(source_dir, file_name), target_dir)
      print(file_name+" was moved")

but the moved file is corrupted, the size is smaller than original file. I think it caused when I've added a new file, it was moved before the file completely added/copied.

Then I tried to use watchdog library, but it was not worked on ftp directory (worked on non-ftp directory).

The file is from camera controller device to my ftp folder on ubuntu, not from my own code using php, python, C#, or another language. So I can't change the source side (camera controller).

Miftakhul Arzak
  • 1,166
  • 1
  • 12
  • 31
  • You could try to open the file in write mode, and if that fails it means the file is still being written. – Mark Ransom Oct 03 '22 at 15:54
  • check answer here: https://stackoverflow.com/a/41105283/9877065 – pippo1980 Oct 03 '22 at 16:01
  • you ae using while True because you start moving files even if ftp is still working ?? – pippo1980 Oct 03 '22 at 16:02
  • @pippo1980 yes, my code start moving files even if ftp is still working. And how to make sure that the file is completely added?And I can't change anything on the camera controller device side. – Miftakhul Arzak Oct 03 '22 at 16:12
  • @pippo1980 `watchdog` library was not worked if I use ftp folder. – Miftakhul Arzak Oct 03 '22 at 16:15
  • first answer doesnt use watchdog library its just a while loop : have a look at it: https://stackoverflow.com/a/41105283/9877065 – pippo1980 Oct 03 '22 at 16:36
  • see here too https://www.unix.com/linux/26367-vsftpd-hiding-partial-uploads.html , https://serverfault.com/questions/141361/how-to-hide-files-starting-with-dot-in-ftp – pippo1980 Oct 03 '22 at 17:06

0 Answers0