Python Forum
While loop - what am i missing? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: While loop - what am i missing? (/thread-6932.html)



While loop - what am i missing? - torz - Dec-14-2017



Hi all,

I'm rather new (obviously) to python, and I've been trying to work out how to get the below code to work properly / work out a different way of doing what I need.

Basically I'm getting a list of files from a remote server, all files will start with EXTRACT_INPUT_ followed by a date formatted like DDMMYYYY. I need to then make sure the file that I want to place does not have the same name so I'm just subtracting 1 day at a time until it does not exit and then that would be what I name my file.



import arrow


def test():
    datetoday = arrow.now().format('DDMMYYYY')
    filetoplace = "EXTRACT_INPUT_" + datetoday + ".TXT"
    remote_files = ['EXTRACT_INPUT_13122017.TXT', 'EXTRACT_INPUT_12122017.TXT', 'EXTRACT_INPUT_14122017.TXT']
    while filetoplace in remote_files:
        filetoplace = "EXTRACT_INPUT_" + arrow.get(datetoday, 'DDMMYYYY').replace(days=-1).format('DDMMYYYY') + ".TXT"
        print(filetoplace)


test()
The code above does change the filetoplace variable to EXTRACT_INPUT_13122017.TXT, but it just keeps printing it over and over again.

My expected result is that the filetoplace variable ends up as EXTRACT_INPUT_11122017.TXT

Many thanks in advance!


RE: While loop - what am i missing? - hshivaraj - Dec-14-2017

What you need is not while. It for

import arrow
 
 
def test():
    datetoday = arrow.now().format('DDMMYYYY')
    filetoplace = "EXTRACT_INPUT_" + datetoday + ".TXT"
    remote_files = ['EXTRACT_INPUT_13122017.TXT', 'EXTRACT_INPUT_12122017.TXT', 'EXTRACT_INPUT_14122017.TXT']
    for filetoplace in remote_files:
        filetoplace = "EXTRACT_INPUT_" + arrow.get(datetoday, 'DDMMYYYY').replace(days=-1).format('DDMMYYYY') + ".TXT"
        print(filetoplace)
 
 
test()



RE: While loop - what am i missing? - torz - Dec-14-2017

Hi hshivaraj,

Thanks for the reply, I did try something similar but it didn't work either...

When I ran your code all I get is 3 lots of EXTRACT_INPUT_13122017.TXT printed instead of the expected EXTRACT_INPUT_11122017.TXT, so it is still not quite what I'm after...


RE: While loop - what am i missing? - squenson - Dec-14-2017

You want to go through decreasing dates, this certainly means a loop and as you don't know when it ends, while seems to be adapted.
You want to compare the file name to a date, this certainly means an if statement.
In short, your program should be something like this:

remote_files = ['EXTRACT_INPUT_13122017.TXT', 'EXTRACT_INPUT_12122017.TXT', 'EXTRACT_INPUT_14122017.TXT']
mydate = currentdate #(in format "DDMMYYYY")
while true: # we loop forever, until something breaks the loop
    filetoplace = "EXTRACT_INPUT_" + mydate + ".TXT"
    if filetoplace in remote_files:
        mydate = mydate - one day # of course, adapt this in real python language!!!
    else:
        # the filetoplace doesn't exist, do what you need to do!
        break # this is breaking the while loop



RE: While loop - what am i missing? - torz - Dec-14-2017

Thanks heaps squenson - believe it or not this is basically what I had originally, not quite sure where I went wrong with what I had before I resorted to the forum :)

below is the working code in case it helps someone else...

import arrow

remote_files = ['EXTRACT_INPUT_13122017.TXT', 'EXTRACT_INPUT_12122017.TXT', 'EXTRACT_INPUT_14122017.TXT','EXTRACT_INPUT_15122017.TXT']
mydate = arrow.now().format('DDMMYYYY')
print("Start date: " + mydate)
while True: # we loop forever, until something breaks the loop
    filetoplace = "EXTRACT_INPUT_" + mydate + ".TXT"
    if filetoplace in remote_files:
        mydate = arrow.get(mydate, 'DDMMYYYY').replace(days=-1).format('DDMMYYYY')
        print("New date: " + mydate)
    else:
        # the filetoplace doesn't exist, do what you need to do!
        break # this is breaking the while loop
print("Filename to use " + filetoplace)