lundi 7 août 2017

Python, store filename outside of function to check upon other iteration

I'm trying to read incoming files from a directory every 3 minutes. Sometimes the files don't get sent due to issues on the other end of the incoming data. I am saving the data from these files onto a separate text file and I would like to be able to check the filename and make sure the program is not inputting the same data twice. To get the program to read the files every 3 minutes I am using the schedule module and am having some issues storing the most recent filename and checking it. My idea for the code is like:

def check(x):
    if x is None:
        check = 0
    else:
        x = check
    return check

def examplefunc():
    path = os.path(r'C:\Users\somedirectory')
    os.chdir(path)
    for i in os.listdir(path):
        if os.path.isfile(os.path.join(path,i)) and 'Testing' in i:
            filelist = [i]
    FileHandle = filelist[0]
    CurrentFile = FileHandle

    CheckFile = check(x)           

    if CurrentFile != CheckFile: 
        print('This is a new file')
        CheckFile = check(CurrentFile)
    else:
        print('This file has already been used')

schedule.every(180).seconds.do(examplefunc)

while 1:
schedule.run_pending()
time.sleep(1)

I'm not sure if this is the correct way to store the variable in the check() function so it can be used every time the function is run. I've also tried using global variable but as the schedule accepts a function I haven't been able to get it to work. Hopefully this makes some sort of sense and any help is greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire