1

The code below 1. Identifies files that are created in a directory and 2. Uploads them to my webserver.

My problem is that the program is only successful when I copy and paste a file into the directory "path_to_watch". The program fails when I use a third-party program (NBA Live 06) that creates files in the "path_to_watch" directory.

The error I receive is: "PermissionError: [Errno 13] Permission denied: 'filename.txt'"

import os, time
from ftplib import FTP

def idFiles():
    path_to_watch = r"c:\Users\User\gamestats"

    before = dict ([(f, None) for f in os.listdir (path_to_watch)])

    while True:
        time.sleep (1)

        after = dict ([(f, None) for f in os.listdir (path_to_watch)])
        added = [f for f in after if not f in before]

        if added: 
            ## edit filename to prepare for upload
            upload = str(", ".join (added))

            ftp = FTP('www.website.com')
            ftp.login(user='username', passwd='password')
            ftp.cwd('archives')

            ## error is called on this following line
            ftp.storbinary('STOR ' + upload, open(upload, 'rb'))

        #resets timer
        before = after

idFiles()

Many thanks in advance for any help.

INoble
  • 67
  • 1
  • 2
  • 8

1 Answers1

0

If the third party program has opened the files in an exclusive mode (which is the default) then you cannot open them yourself until it has let go of them.

Considering that it's third party code you can't change the mode in which the file is open, but you'll have to wait for the program to close the files before trying to manipulate them.

See also this question

miniBill
  • 1,743
  • 17
  • 41