Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

ftplib

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?

like image 933
Matt Avatar asked May 23 '17 13:05

Matt


People also ask

How do you fix TypeError expected str bytes or OS PathLike object not Bytesio?

The Python "TypeError: expected str, bytes or os. PathLike object, not TextIOWrapper" occurs when we pass a file object instead of a string when opening a file. To solve the error, pass the filename (as a string) to the open() function.

How do you fix TypeError expected str bytes or OS PathLike object not a NoneType?

The Python "TypeError: expected str, bytes or os. PathLike object, not NoneType" occurs when we try to open a file but provide a None value for the filename. To solve the error, figure out where the None value comes from and correct the assignment.

What is PathLike?

PathLike. An abstract base class for objects representing a file system path, e.g. pathlib.PurePath . New in version 3.6. abstractmethod __fspath__ () Return the file system path representation of the object.


1 Answers

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

like image 118
chron0x Avatar answered Oct 22 '22 00:10

chron0x