Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python EOFError-使用ftplib上载.csv文件_Python_Python 3.x_Csv_Ftplib - Fatal编程技术网

Python EOFError-使用ftplib上载.csv文件

Python EOFError-使用ftplib上载.csv文件,python,python-3.x,csv,ftplib,Python,Python 3.x,Csv,Ftplib,我正在尝试使用python3上的ftplib上传.csv文件。 我的代码如下所示: from ftplib import FTP_TLS import os, sys def get_script_path(): return os.path.dirname(os.path.realpath(sys.argv[0])) script_path = get_script_path() ftp = FTP_TLS(host='hostedftp.com') ftp.login('USER

我正在尝试使用python3上的ftplib上传.csv文件。 我的代码如下所示:

from ftplib import FTP_TLS
import os, sys

def get_script_path():
    return os.path.dirname(os.path.realpath(sys.argv[0]))

script_path = get_script_path()
ftp = FTP_TLS(host='hostedftp.com')
ftp.login('USER','123456789')
ftp.prot_p() 

filename = script_path + '/Test.csv'
fp = open(filename, 'r')
ftp.storlines("STOR " + filename, fp)

ftp.close()
我得到:

文件“ftp_test.py”,第15行,在 ftp.storlines(“STOR”+文件名,fp)。。。文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ftplib.py”, 第208行,在getline中 提高EOFEROR EOFEROR


知道为什么吗?

我建议您切换到使用二进制模式。这还包括使用
rb
打开文件。例如:

from ftplib import FTP_TLS
import os, sys

def get_script_path():
    return os.path.dirname(os.path.realpath(sys.argv[0]))

script_path = get_script_path()
ftp = FTP_TLS(host='hostedftp.com')
ftp.login('USER','123456789')
ftp.prot_p() 

filename = 'Test.csv'

with open(os.path.join(get_script_path(), filename), 'rb') as fp:
    try:
        ftp.storbinary("STOR " + filename, fp)
        ftp.quit()   # This can raise EOFError if the connection has closed 
    except EOFError:
        pass

ftp.close() 

如果文件上传正常,您还可以捕获
eoferor
,如果连接已关闭,则会引发该问题。

问题在于我使用的是STOR命令的完整路径。 以下是固定代码:

from ftplib import FTP_TLS
import os, sys

def get_script_path():
    return os.path.dirname(os.path.realpath(sys.argv[0]))

script_path = get_script_path()

ftp = FTP_TLS(host='hostedftp.com')
ftp.login('USER','123456789')
ftp.prot_p() 
filename_path = script_path + '/Test.csv'
filename = 'Test.csv'
fp = open(filename_path, 'rb')
ftp.storbinary("STOR " + filename, fp)
fp.close()
ftp.quit()

请注意,我是如何使用
filename\u路径打开它并获取STOR命令的
fp
,但实际的
filename

您是否尝试过
ftp.storbinary
?也许你的文件中有一个非ASCII字符,我们能看到一个示例吗?是的@TomasFarias,但我得到了相同的错误。我得到了相同的错误,但在ftp.quit()行。