I am trying to find a specific folder holding a bunch of fits files. The current code I have is
redpath = os.path.realpath('.')
thispath = os.path.realpath(redpath)
thispath = os.path.realpath(thispath+'/../../../..')
p = Path(thispath)
userinput = 'n'
while (userinput == 'n'):
text_file = next(p.glob('**/*.fits'))
print("Is this the correct file path?")
print(text_file)
SearchedFiles = []
SearchedFiles.append(text_file)
userinput = input("y or n")
if (userinput == 'n') :
while(text_file in SearchedFiles) :
p = Path(thispath)
text_file = next(p.glob('**/*.fits'))
So if pathlib finds the wrong file fist, the user will say so and supposedly the code will then go through and search again until it finds another file with fits folders. I am getting stuck in an infinite loop because it only goes down one path.
I'm not quite sure I understand what you're trying to do.
However, it's no wonder you're getting stuck in a loop: by re-initializing p.glob()
you're starting all over again every time!
p.glob()
is actually a generator object, which means it will keep track of its progress by itself. You may just use it the way it was meant to be used: by just iterating over it.
So, for instance, you might be better served by the following:
redpath = os.path.realpath('.')
thispath = os.path.realpath(redpath)
thispath = os.path.realpath(thispath+'/../../../..')
p = Path(thispath)
chosen = None
for text_file in p.glob('**/*.fits'):
print("Is this the correct file path?")
print(text_file)
userinput = input("y or n")
if userinput == 'y':
chosen = text_file
break
if chosen:
print ("You chose: " + str(chosen))