• Register
0 votes
3.9k views

Problem :

I am very new to python. I am doing file handling for the first time. I am trying to iterate through the group of files in my folder on my local machine and upload only ones where a file names contain "Service_Areas" to the FTP site using my below code.

 Please note : I am using Python 3.6.1 32 bit, Windows 10 64 bit

upftp = FTP('ftp.ftpsite.org')
upusername = ('username')
uppassword = ('password')
upftp.login(upusername,uppassword)
upftp.cwd(upusername.upper())
upftp.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')
        upftp.storbinary("STOR " + i, open(file, 'rb'))
        file.close()
        continue
    else:
        print('nope')

upftp.quit()

However I am getting below error:

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

Any suggestions how can I fix the error?

7 5 2
3,870 points

1 Answer

0 votes

Solution :

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

I hope above suggestion will help you in resolving your error.

9 7 4
38,600 points

Related questions

1 vote
1 answer 254 views
254 views
Problem : I am learning to use Python. Currently I want to iterate through the files which are present in the folder on the local machine and then want to upload only those files whose file names have "Service_Areas" to the required FTP site with the use of ... rb')) TypeError: expected str, bytes or os.PathLike object, not _io.BufferedReader Please guide me in fixing my python ftp related error.
asked May 14 Martin K 6.6k points
0 votes
2 answers 938 views
938 views
Problem : I am new to Py 3.5. This code was working properly in Python 2.7: with open(fname, 'rb') as f: lines = [x.strip() for x in f.readlines()] for line in lines: tmp = line.strip().lower() if 'some-pattern' in tmp: continue # ... code After upgrading to 3.5, I'm getting the: TypeError: a bytes-like object is required, not 'str'
asked Oct 22, 2019 peterlaw 6.9k points
1 vote
1 answer 58 views
58 views
Problem: I wrote a program in which gives me the following error : TypeError: startswith first arg must be bytes or a tuple of bytes, not str The program I wrote is: def allFasta(filename): xd = open(filename, 'yb') headers = "" sequence = "" while True: ... ("youn.xd") Why is this type error message is appearing for this program. Is there any problem with my programs arguments? Thanks in advance.
asked Jun 11 samhaz 5k points
0 votes
1 answer 1.5K views
1.5K views
Problem : I am facing the issue in passing a function as a parameter to another function as follows typeerror: int() argument must be a string, a bytes-like object or a number, not 'nonetype'
asked Nov 18, 2019 peterlaw 6.9k points
1 vote
2 answers 1.4K views
1.4K views
Problem : I am very new to Python. Currently I have one textfile and now I am trying to load it and then want to make the list which should contain each and every line from the text file. I have already tried to use the split() procedure. But I am facing ... I have already wasted a lot of time in understanding and finding workaround for above error.So I am looking for the quick fix on above error.
asked Apr 8 morrisBson 3.2k points