2016-07-28 21 views
-1

我的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") 

是否有人知道如何讓文件被髮送一個以不同的文件名的時間。文件發送後,它會立即被刪除。

回答

0

只是要的文件列表,你想要傳輸,然後遍歷該列表逐個發送文件。

這裏一個簡單的函數來獲取文件的列表:

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) 
+0

嗨亞歷克,我得到了錯誤,聲明NameError:name list_of_files未定義 –

+0

您是否在運行之前定義了該函數它? –

+0

我是新來的python,所以我不知道該怎麼做。 「我應該爲」將每個文件的文件路徑列表返回到具有目標擴展名的文件夾中「添加什麼內容? –

0

也許可以用列表來保存文件,如:影像=「/ tmp目錄/圖片/ frame.png」,「」] 然後在圖像使用X: