TypeError:预期的str,bytes或os.PathLike对象,而不是_io.BufferedReader

Mat*_*att 18 python ftplib

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

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()
Run Code Online (Sandbox Code Playgroud)

但我收到这个错误:

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
Run Code Online (Sandbox Code Playgroud)

有什么建议?

chr*_*n0x 12

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