-1

i would like to check in a while loop if files exist in directory, but I haven't idea. I think is easy but I have problem.

root_path = 'C:/Users/Desktop/Files/'
date_now = datetime.datetime.now().strftime("%Y%m%d")

if not os.listdir(root_path):
    print("Directory is empty")
else:
    print("Directory is not empty")
    cmd = 'scp ' + root_path + '*.sql ...'
    ret1, status1 = tools.cmd(cmd)
    print('ret1: ' + ret1)

    while fileExists in root_path do :
  • To check if a path (file or directory) exists, use `while pathlib.Path(my_file_path).exists(): ...`. Docs [here](https://docs.python.org/3/library/pathlib.html#pathlib.Path.exists). – Rick Jun 22 '23 at 20:25

1 Answers1

0

Are you trying to execute a block of code while a file exists in your root_path directory?

In this case your while clause should be

while os.listdir(root_path):
    your_code_here

In this case your_code_here will be executed while the root_path has at least one file in there.

Be careful with this approach, because you might get into a perpetual while loop here

STK
  • 102
  • 4
  • 11