类型错误:期望字符串、字节或类似于os.Path的对象,而不是_io.BufferedReader。

41
我想遍历本地计算机上一个文件夹中的一组文件,并仅上传文件名包含“Service_Areas”的文件到我的 FTP 网站,使用以下代码(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()
但是我遇到了这个错误:
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

有什么建议吗?

1个回答

23

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接