【问题标题】:How to file open with different filename using python ftplib如何使用 python ftplib 以不同的文件名打开文件
【发布时间】:2026-01-16 07:05:02
【问题描述】:

我目前的python代码:

import ftplib
import hashlib
import urllib

def ftp():
  hashing="123"
  ftp = ftplib.FTP('localhost','kevin403','S$ip1234')
  ftp.cwd('/var/www/html/image')

  m=hashlib.md5()
  file = open('Desktop/test.png','rb')
  m.update(hashing)
  dd = m.hexdigest()
  ftp.storbinary('STOR '+dd+ '.png', file)

  file.close()
  ftp.quit()

我得到了由 test.png、test1.png 和 test2.png 组成的不同文件名。我想打开文件并在任何文件打开时存储它。 我尝试使用 * 星号,但出现错误:

file = open('Desktop/*.png, 'rb') 

【问题讨论】:

  • 我很困惑,你有test1.png, test2.png, 等文件吗?如果是这样,那么首先为他们提供全局并列出他们?然后循环遍历列表。可能是我理解错了,你的问题不是很清楚。
  • 这 3 个文件在我的桌面上。
  • 您希望 ftp 函数查找所有这些文件还是每次调用该函数处理一个文件?
  • 是的,我想这样做
  • 在下面查看我的答案

标签: python python-2.7 ftp


【解决方案1】:

试试这个版本:

import os
import ftplib
import hashlib
import glob

image_directory = '/home/Desktop/images/'

hashing = "123"
m = hashlib.md5()
m.update(hashing)
dd = m.hexdigest()

ftp = ftplib.FTP('localhost','kevin403','S$ip1234')
ftp.cwd('/var/www/html/image')

for image in glob.iglob(os.path.join(image_directory, 'test*.png')):
   with open(image, 'rb') as file:
      ftp.storbinary('STOR '+dd+ '.png', file)

ftp.close()
ftp.quit()

我希望你意识到这只会在你的 FTP 服务器上一遍又一遍地写入同一个文件,因为dd 永远不会更新。

您也真的不应该再使用 MD5。如果您只想存储带有校验和的文件,请尝试使用此版本:

import os
import ftplib
import hashlib
import glob

def get_sha512(file):
    f = open(file, 'rb')
    value = hashlib.sha256(f.read()).digest()
    f.close()
    return value

image_directory = '/home/Desktop/images/'

ftp = ftplib.FTP('localhost','kevin403','S$ip1234')
ftp.cwd('/var/www/html/image')

for image in glob.iglob(os.path.join(image_directory, 'test*.png')):
   hash = get_sha512(image)[:16]
   with open(image, 'rb') as file:
      ftp.storbinary('STOR {}.png'.format(hash), file)

ftp.close()
ftp.quit()

【讨论】:

  • iglob 更高效
  • 当我运行脚本时,我得到了这个错误: Traceback (last recent call last): File "/home/kevin403/ftpttest.py", line 17, in for image in glob.iglob(os.path.join(image_directory, 'test*.png')): NameError: name 'glob' is not defined
  • 很抱歉。
  • 现在我得到了这个错误:AttributeError: 'NoneType' object has no attribute 'sendall'
【解决方案2】:

这个怎么样?使用 glob 查找文件,然后从 glob 对每个文件运行 ftp() 遍历列表?

import ftplib
import hashlib
import urllib
from glob import glob

def ftp(filename):
  hashing="123"
  ftp = ftplib.FTP('localhost','kevin403','S$ip1234')
  ftp.cwd('/var/www/html/image')

  m=hashlib.md5()
  file = open(filename,'rb')
  m.update(hashing)
  dd = m.hexdigest()
  ftp.storbinary('STOR '+dd+ '.png', file)

  file.close()
  ftp.quit()

if __name__ == '__main__':
   # get the files
   templist = glob('test*.png')

   # loop over the files we found
   for filename in templist:
      ftp(filename)

【讨论】:

  • 如何运行整个程序?我收到了这个错误:TypeError: ftp() 只需要 1 个参数(给定 0)
  • 复制我放入文件的所有内容并运行它: python myfile.py 在与 pngs 相同的目录中(我已经更新了代码)在上面的错误中,你仍然没有传递文件名到 ftp() 函数。答案在错误中。它现在需要 1 个参数(文件名),而你给了它 0。函数下面的行是将这个参数传递给 ftp()
  • 我尝试以这种方式运行:import ftpttest 然后打印 ftp(filename),我得到的错误是文件名未定义。
  • 当然文件名没有定义,你没有创建临时列表并循环它!你不能选择解决方案的一部分,那么它就不是解决方案。我已经为你提供了一个完整的示例代码,但你拒绝运行它并正确测试它......祝你好运。
最近更新 更多