一次发送一个文件?
问题描述:
我的python脚本:一次发送一个文件?
#!/usr/bin/python
import CameraID
import time
import os
image="/tmp/image/frame.png"
count = 1
while (count==1):
# make sure file file exists, else show an error
if (not os.path.isfile(image)):
print("Error: %s file not found" %image)
else:
print("Sending file %s ..." % image)
print CameraID.run()
print os.remove("/tmp/image/frame.png")
是否有人知道如何让文件被发送一个以不同的文件名的时间。文件发送后,它会立即被删除。
答
只是要的文件列表,你想要传输,然后遍历该列表逐个发送文件。
这里一个简单的函数来获取文件的列表:
def list_of_files(folder, extension):
'''
Return a list of file-paths for each file into a folder with the target extension.
'''
import glob
return glob.glob(str(folder + '*.' + extension))
你的情况
这将是:
files = list_of_files('/tmp/image/','png')
for image in files:
if (not os.path.isfile(image)):
print("Error: %s file not found" %image)
else:
print("Sending file %s ..." % image)
print CameraID.run()
print os.remove(image)
没有函数定义:
import glob
files = glob.glob('/tmp/image/*.png')
for image in files:
if (not os.path.isfile(image)):
print("Error: %s file not found" %image)
else:
print("Sending file %s ..." % image)
print CameraID.run()
print os.remove(image)
答
也许可以用列表来保存文件,如:影像=“/ tmp目录/图片/ frame.png”,“”] 然后在图像使用X:
嗨亚历克,我得到了错误,声明NameError:name list_of_files未定义 –
您是否在运行之前定义了该函数它? –
我是新来的python,所以我不知道该怎么做。 “我应该为”将每个文件的文件路径列表返回到具有目标扩展名的文件夹中“添加什么内容? –