Python 文件上传器*有时*拒绝许可
Python File Uploader denying permission *some* of the time
下面的代码 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()
非常感谢您的帮助。
如果第三方程序以独占模式(默认模式)打开文件,那么您无法自己打开它们,直到它释放它们。
考虑到它是第三方代码,您无法更改文件的打开模式,但您必须等待程序关闭文件,然后才能尝试操作它们。
另见 this question
下面的代码 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()
非常感谢您的帮助。
如果第三方程序以独占模式(默认模式)打开文件,那么您无法自己打开它们,直到它释放它们。
考虑到它是第三方代码,您无法更改文件的打开模式,但您必须等待程序关闭文件,然后才能尝试操作它们。
另见 this question