Allen.Kan Allen.Kan - 3 months ago 6
Python Question

Send one file at a time?

My python script:

#!/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")


Does anybody know how to allow the file to be send one at a time with different filename. Once the file is send, it will be removed instantly.

Answer

Just make a list of files you want to transfer, then iterate over that list to send the files one by one.

Here a simple function to get the list of files:

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

in your case it would be:

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)

Without function definition:

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