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

2

42

I'm trying to iterate through a group of files in a folder on my local machine and upload only ones where the file names contain "Service_Areas" to my FTP site using using this code (Python 3.6.1 32 bit, Windows 10 64 bit):

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()

but I am getting this error:

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

Any suggestions?

Trimaran answered 23/5, 2017 at 13:15 Comment(0)
F
24

I think it has to do with your second element in storbinary. You are trying to open file, but it is already a pointer to the file you opened in line file = open(local_path,'rb'). So, try to use ftp.storbinary("STOR " + i, file).

Fluttery answered 23/5, 2017 at 13:29 Comment(0)
P
0

Just for those coming from Django framework platform

If your MEDIA_ROOT has been configured as a list. make sure to remove the list object to expose the path as shown below

# MEDIA_ROOT = [os.path.join(BASE_DIR, 'media')]

change it to

   MEDIA_ROOT= os.path.join(BASE_DIR, 'media')
Parcel answered 22/7 at 22:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.