• Register
0 votes
5.6k 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

Please log in or register to answer this question.

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 742 views
742 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, 2020 Martin K 6.6k points
0 votes
1 answer 77 views
77 views
Problem: i have json file named "panamaleaks50k.json" . I want to get ['text'] field from json file but it shows me following error the JSON object must be str, bytes or bytearray, not 'TextIOWrapper' this is my code with open('C:/Users/bilal butt/Desktop/PanamalEakJson.json','r') as lst: b = json.loads(lst) print(b['text'])
asked Feb 18 Mashhoodch 9.6k points
0 votes
1 answer 16 views
16 views
Problem: typeerror: '_io.textiowrapper' object is not subscriptable.
asked Feb 19 Dan phillip 1.9k points
0 votes
2 answers 1.2K views
1.2K 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 422 views
422 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, 2020 samhaz 5k points
1 vote
2 answers 3.2K views
3.2K 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, 2020 morrisBson 3.2k points
0 votes
1 answer 2.1K views
2.1K 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
0 votes
1 answer 11 views
11 views
Problem: I was looking for a way to run an external process from python script and print its stdout messages during the execution. The code below works, but prints no stdout output during runtime.
asked Feb 20 Mashhoodch 9.6k points
0 votes
1 answer 16 views
16 views
Problem: I am trying to connect to a server using a socket module in Python. However, I am getting an error like below. TypeError: a bytes-like object is required, not 'str' Here is my code: import socket HOST = '0.0.0.0' PORT = 12345 server_socket = socket.socket ... is causing this TypeError? and what should I do to fix it? I'm also open to any advice on how to improve your Python socket coding.
asked Feb 7 sasha 13.2k points