gpt4 book ai didi

python - 如何使用python读取ftp上的文件?

转载 作者:行者123 更新时间:2023-11-30 23:08:22 30 4
gpt4 key购买 nike

这是我的代码:

import ftputil
import urllib2
a_host = ftputil.FTPHost(hostname, username,passw)

for (dirname, subdirs, files) in a_host.walk("/"): # directory
for f in files:
if f.endswith('txt'):
htmlfile = open(f, 'r')
readfile = htmlfile.read()

我认为应该没问题,但我遇到了错误

Traceback (most recent call last):

htmlfile = open(f, 'r')
IOError: [Errno 2] No such file or directory: u'readme.txt'

问题出在哪里?

最佳答案

您无法像本地文件一样使用open读取远程文件。您需要先从远程主机下载文件。

for (dirname, subdirs, files) in a_host.walk("/"): # directory
for f in files:
if f.endswith('txt'):
a_host.download(f, f) # Download first
with open(f) as txtfile:
content = txtfile.read()

关于python - 如何使用python读取ftp上的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31759805/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com