Python 类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是 _io.BufferedReader
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44135947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: expected str, bytes or os.PathLike object, not _io.BufferedReader
提问by Matt
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):
我正在尝试遍历本地计算机上文件夹中的一组文件,并使用此代码(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()
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?
有什么建议?
采纳答案by chron0x
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)
.
我认为这与您在storbinary
. 您正在尝试打开file
,但它已经是指向您在行中打开的文件的指针file = open(local_path,'rb')
。因此,尝试使用ftp.storbinary("STOR " + i, file)
.