Using Python to know when a file was completely received from an FTP source

I use Python to develop an application that does the following:

  • Monitors a specific directory and ensures that the file is transferred to it. Once the file completes the transfer, run a few external program in the file.

The main problem I'm developing this application with is knowing when the file completed the transfer. From what I know, the file will be transferred via SFTP to a specific directory. How does Python know when the file completes the transfer? I know that I can use the st_size attribute from the object returned by the os.stat(fileName) method. Are there any other tools that I need to use to achieve these goals?

+4
source share
2 answers

In the end, I used a watchdog timer combination and waited until I could open the file for writing

  #If there is no error when trying to read the file, then it has completely loaded try: with io.FileIO(fileName, "r+") as fileObj: ''' Deal with case where FTP client uses extensions such as ".part" and '.filepart" for part of the incomplete downloaded file. To do this, make sure file exists before adding it to list of completedFiles. ''' if(os.path.isfile(fileName)): completedFiles.append(fileName) print "File=" + fileName + " has completely loaded." except IOError as ioe: print str(ioe) 
+6
source

The best way to resolve this issue would be to transfer the SFTP transmission to the waiting area, and then (presumably using SSH) issue the mv command to move the file from the storage to the destination. Then, as soon as the file appears in the destination area, your script knows that it is completely migrated.

+3
source

Source: https://habr.com/ru/post/1499510/


All Articles