Python 卡在环上了!
我正在创建一个应用程序,用于将图像上载到指定的服务器。我已经在Qt Designer中创建了我的GUI,一切都很好,我只是停留在一些我知道很简单的东西上。我似乎不能把我的头绕在它周围 这个想法是让脚本遍历并查看有多少文本字段与图像路径一起归档——从中获取每个路径,然后将每个路径上传到服务器。我只需要一个盒子就可以很好地工作,但是当我尝试为这个过程创建一个循环时,它就崩溃了。我基本上需要返回每个不同路径的“全名”。这只是小题大做,但你明白了 这个概念似乎很简单,我已经用我能找到和想到的很多方法重写了它。任何帮助都会很棒。我应该用列表来代替吗Python 卡在环上了!,python,image,ftp,range,qt-designer,Python,Image,Ftp,Range,Qt Designer,我正在创建一个应用程序,用于将图像上载到指定的服务器。我已经在Qt Designer中创建了我的GUI,一切都很好,我只是停留在一些我知道很简单的东西上。我似乎不能把我的头绕在它周围 这个想法是让脚本遍历并查看有多少文本字段与图像路径一起归档——从中获取每个路径,然后将每个路径上传到服务器。我只需要一个盒子就可以很好地工作,但是当我尝试为这个过程创建一个循环时,它就崩溃了。我基本上需要返回每个不同路径的“全名”。这只是小题大做,但你明白了 这个概念似乎很简单,我已经用我能找到和想到的很多方法重写
# count how many images there are going to be
if not self.imgOnePathLabel.text().isEmpty():
totalImages = 1
# gets the path from IMAGE 1 box
image1 = self.imgOnePathLabel.text()
fullname = '%s' % image1
if not self.imgTwoPathLabel.text().isEmpty():
totalImages = 2
image2 = self.img2PathLabel.text()
fullname = '%s' % image2
if not self.imgThreePathLabel.text().isEmpty():
totalImages = 3
imageThreePath = self.imgThreePathLabel.text()
fullname = '%s' % imageThreePath
try:
for x in range(1,totalImages,1):
# split end file from the file path
name = os.path.split(fullname)[1]
f = open(fullname, "rb")
# store our selected file
ftp.storbinary('STOR ' + name, f)
msg = "Sent <font color=green>" + name + "</font>"
self.logBrowser.append(msg)
f.close()
finally:
msg = "<font color=green>" "Ok" "</font>"
self.logBrowser.append(msg)
#计算将有多少图像
如果不是self.imgOnePathLabel.text().isEmpty():
totalImages=1
#从“图像1”框中获取路径
image1=self.imgOnePathLabel.text()
全名=“%s”%image1
如果不是self.imgTwoPathLabel.text().isEmpty():
totalImages=2
image2=self.img2PathLabel.text()
全名=“%s”%image2
如果不是self.imgThreePathLabel.text().isEmpty():
totalImages=3
imageThreePath=self.imgThreePathLabel.text()
全名=“%s”%imageThreePath
尝试:
对于范围内的x(1,totalImages,1):
#从文件路径拆分结束文件
name=os.path.split(全名)[1]
f=打开(全名,“rb”)
#存储我们选择的文件
ftp.storbinary('STOR'+名称,f)
msg=“已发送”+名称+“”
self.logBrowser.append(msg)
f、 关闭()
最后:
msg=“”好的“”
self.logBrowser.append(msg)
除了范围需要为+1以达到#3的问题(参见Vincent R的评论)
(其中一个)问题是fullname变量被每个新的非空标签覆盖
很难只对代码进行修改就对其进行评论,也就是说,我想建议并重新组织它。例如,通过引入一个函数来提取给定图像、给定UI对象的名称/路径;这样可以避免一些重复。这样一个函数,或者它的调用者,将把每个新的全名添加到一个列表中,然后上传循环就可以使用这个列表
参见Tendayi Mawuse的解决方案,该方案尊重原始结构,但按照建议引入了一个列表。顺便说一句,这个列表可以作为循环的基础进行迭代,而不是依赖range()函数,这更像pythonic(这样就不需要修复range中缺少#3的问题)。在Python中,这些数值范围驱动的循环有时很方便,但它们通常是重新审视设计的邀请。您遇到的问题是,您正在为变量
fullname指定三次,每次都覆盖它。因此,当您进入for循环时,如果设置了最后一个字段,则只有最后一个文件名可用,否则将一无所获。您需要一个全名列表而不是一个变量的说法是正确的。您需要以下内容:
fullnames = []
imageLabels = [self.imgOnePathLabel, self.imgTwoPathLabel,
self.imgThreePathLabel]
for imageLabel in imageLabels:
if imageLabel.text():
image = self.imgOnePathLabel.text()
fullnames.append('%s' % image)
try:
for fullname in fullnames:
# split end file from the file path
name = os.path.split(fullname)[1]
f = open(fullname, "rb")
# store our selected file
ftp.storbinary('STOR ' + name, f)
msg = "Sent <font color=green>" + name + "</font>"
self.logBrowser.append(msg)
f.close()
finally:
msg = "<font color=green>" "Ok" "</font>"
self.logBrowser.append(msg)
fullnames=[]
imageLabels=[self.imgOnePathLabel,self.imgTwoPathLabel,
self.imgThreePathLabel]
对于imageLabels中的imageLabel:
如果imageLabel.text():
image=self.imgOnePathLabel.text()
全名。追加(“%s”%image)
尝试:
对于全名中的全名:
#从文件路径拆分结束文件
name=os.path.split(全名)[1]
f=打开(全名,“rb”)
#存储我们选择的文件
ftp.storbinary('STOR'+名称,f)
msg=“已发送”+名称+“”
self.logBrowser.append(msg)
f、 关闭()
最后:
msg=“”好的“”
self.logBrowser.append(msg)
原始代码的另一个问题是,如果标签1和3不是空的,但标签2是空的,totalImages
将被设置为3,即使您只有两条路径
此外,此代码中的输入错误(“两个”对“2”):
我相信您不需要字符串替换'%s'%image
您可以将代码压缩一点(并解决问题),如下所示:
# this is a list of references to your PyQt4 label objects
imagePathLabels = [self.imgOnePathLabel,
self.imgTwoPathLabel,
self.imgThreePathLabel]
try:
for label in imagePathLabels:
if not label.text().isEmpty():
image_path = label.text()
image_name = os.path.split(image_path)[1]
f = open(image_path, "rb")
ftp.storbinary('STOR ' + image_name, f)
msg = "Sent <font color=green>" + name + "</font>"
self.logBrowser.append(msg)
f.close()
finally:
msg = "<font color=green>" "Ok" "</font>"
self.logBrowser.append(msg)
#这是对PyQt4标签对象的引用列表
imagePathLabels=[self.imgOnePathLabel,
self.imgTwoPathLabel,
self.imgThreePathLabel]
尝试:
对于imagePathLabels中的标签:
如果不是label.text().isEmpty():
image\u path=label.text()
image\u name=os.path.split(image\u path)[1]
f=打开(图像路径,“rb”)
ftp.storbinary('STOR'+图像名称,f)
msg=“已发送”+名称+“”
self.logBrowser.append(msg)
f、 关闭()
最后:
msg=“”好的“”
self.logBrowser.append(msg)
只需对循环进行注释…如果totalImages为3,x的值将为1和2,但决不会为3。非常感谢!我打赌解决方案与创建列表有关,我只是不确定方法。我明白你的意思了,它确实更像是pythonic感谢Tendayi——它就像一个符咒,我认为我所有的“如果不是”的陈述让它感觉有点笨重,但这是我需要的一个解决方案!你完全可以把这一点删减一点,我最初给出的是一种解决你眼前问题的方法。我对这个解决方案做了一个小的更新,使它读起来更好,并且更容易添加更多的文本字段。
# this is a list of references to your PyQt4 label objects
imagePathLabels = [self.imgOnePathLabel,
self.imgTwoPathLabel,
self.imgThreePathLabel]
try:
for label in imagePathLabels:
if not label.text().isEmpty():
image_path = label.text()
image_name = os.path.split(image_path)[1]
f = open(image_path, "rb")
ftp.storbinary('STOR ' + image_name, f)
msg = "Sent <font color=green>" + name + "</font>"
self.logBrowser.append(msg)
f.close()
finally:
msg = "<font color=green>" "Ok" "</font>"
self.logBrowser.append(msg)