如何使用python ftplib以不同的文件名打开文件

如何使用python ftplib以不同的文件名打开文件,python,python-2.7,ftp,Python,Python 2.7,Ftp,我当前的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)

我当前的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') 

这个怎么样?使用glob查找文件,然后在每个文件上运行ftp()的glob的列表上循环

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)

请尝试此版本:

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()
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()
我希望您意识到这将在FTP服务器上反复写入相同的文件,因为
dd
永远不会更新

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

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()
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()

我很困惑,您是否已经有了文件
test1.png、test2.png、
等?如果是这样的话,那么首先为他们做一个glob并列出他们的名单?然后在列表上循环。可能是我误解了,你的问题不清楚。这3个文件在我的桌面上。你想让ftp功能查找所有这些文件,还是每次调用该功能都处理一个文件?是的,我想这样做。下面是我的答案。你想运行整个程序吗?我得到了这个错误:TypeError:ftp()正好接受1个参数(给定0)将我放入的所有内容复制到一个文件中,并使用:python myfile.py在与上面错误中的PNG(我已更新代码)相同的目录中运行它。您仍然没有将文件名传递给ftp()函数。答案是错误的。它现在需要1个参数(文件名),而您给了它0。函数下面的几行是将此参数传递给ftp()的代码。我尝试以以下方式运行:导入ftpttest,然后打印ftp(filename),我得到的错误是没有定义filename。当然filename没有定义,您没有创建模板并在其上循环!您不能选择解决方案的一部分,因为它不是一个解决方案。我提供了一个完整的示例代码,但您拒绝运行它并正确测试它。。。good luck.iglob在我运行脚本时效率更高,我得到了以下错误:回溯(最近一次调用):文件“/home/kevin403/ftpttest.py”,第17行,用于glob.iglob(os.path.join(image_目录,'test*.png')中的图像):NameError:name'glob'未定义操作很抱歉。现在我遇到了以下错误:AttributeError:'NoneType'对象没有属性'sendall'