Python Forum
While loop - what am i missing?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While loop - what am i missing?
#1


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!
Reply
#2
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()
Reply
#3
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...
Reply
#4
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
Reply
#5
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)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020