Python File Uploader拒绝*某些*的时间

时间:2015-09-23 20:35:27

标签: python upload python-3.4 ftplib

以下代码1.识别在目录中创建的文件,然后2.将它们上传到我的网络服务器。

我的问题是,当我将文件复制并粘贴到目录“path_to_watch”时,程序才会成功。当我使用在“path_to_watch”目录中创建文件的第三方程序(NBA Live 06)时,程序失败。

我收到的错误是:“PermissionError:[Errno 13]权限被拒绝:'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()

非常感谢您的任何帮助。

1 个答案:

答案 0 :(得分:0)

如果第三方程序以独占模式打开文件(这是默认设置),那么在它放开之前你不能自己打开它们。

考虑到它的第三方代码,您无法更改文件打开的模式,但在尝试操作文件之前,您必须等待程序关闭文件

另见this question