我正在尝试找到一个包含一堆适合文件的特定文件夹。我目前的代码是
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'))
因此,如果 pathlib 找到了错误的文件,用户会这样说,并且假设代码将通过并再次搜索,直到找到另一个包含适合文件夹的文件。我陷入了无限循环,因为它只走一条路。