TypeError: expected str, bytes or os.PathLike object, not _io.BufferedReader

TypeError: expected str, bytes or os.PathLike object, not _io.BufferedReader

我正在尝试遍历本地计算机上文件夹中的一组文件,并使用此代码仅将文件名包含 "Service_Areas" 的文件上传到我的 FTP 站点( Python 3.6.1 32 位,Windows 10 64 位):

ftp = FTP('ftp.ftpsite.org')
username = ('username')
password = ('password')
ftp.login(username,password)
ftp.cwd(username.upper())
ftp.cwd('2017_05_02')

for i in os.listdir('C:\FTP_testing'):
    if i.startswith("Service_Area"):
        local_path = os.path.join('C:\FTP_testing',i)
        file = open(local_path,'rb')
        ftp.storbinary("STOR " + i, open(file, 'rb'))
        file.close()
        continue
    else:
        print('nope')

ftp.quit()

但我收到此错误:

Traceback (most recent call last):
  File "C:\Users\user\Desktop\Test1.py", line 32, in <module>
    ftp.storbinary("STOR " + str(i), open(file, 'rb'))
TypeError: expected str, bytes or os.PathLike object, not _io.BufferedReader

有什么建议吗?

我认为这与 storbinary 中的第二个元素有关。您正在尝试打开 file,但它已经是指向您在行 file = open(local_path,'rb') 中打开的文件的指针。所以,尝试使用 ftp.storbinary("STOR " + i, file).