【问题标题】:Python File Uploader denying permission *some* of the timePython File Uploader 有时*某些*拒绝权限
【发布时间】:2015-09-23 20:35:26
【问题描述】:

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

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

我收到的错误是:“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()

非常感谢您的帮助。

【问题讨论】:

    标签: python upload python-3.4 ftplib


    【解决方案1】:

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

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

    另见this question

    【讨论】:

    • 谢谢!我不知道发生了这种情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 2015-01-02
    • 1970-01-01
    相关资源
    最近更新 更多