Home > Mobile >  Python - error expected in while loop but is not shown
Python - error expected in while loop but is not shown

Time:08-20

import os
import time

path = os.listdir('/home/dc4/Downloads/DEB/')

while True:
    check = '.part' in path[1] # x[1] == 'grape.part' (this is also a file)
    
    if check is True:
        print('True')
        time.sleep(5)
    

I am using os module and its listdir() method to list all files in the variable path, the first line inside while loop checks if the file extension .part is in the string 'grape.part' and if the file is located in the path directory.

In this image --> https://i.stack.imgur.com/uHDRh.png .part is in the 'grape.part' which is also in the path directory, the if statement checks this and it prints True and uses the time module to sleep every 5 seconds as expected.

However if I remove grape.part from the path directory while the program is running there is no error which I am expecting, it just continues printing True until I stop the program --> https://i.stack.imgur.com/uxqme.png

If I stop and start the program when grape.part is not in the path directory only then do I get the error why and how do I fix this? (I don't know if I explained the issue well I'm pretty new to python)

CodePudding user response:

Do you want to check specifically for that file or any file with that extension? This first solution checks for the file.

I've added an else statement so the program knows to break when the file is removed. I've also used Path from pathlib to change how you check that your file exists.

The reason your implementation didn't work is that you were checking whether the check list you created contained the file and not the directory itself.

import os
import time
from pathlib import Path

file_path = Path('/home/dc4/Downloads/DEB/grape.path')
while True:
    check = file_path.exists()

    if check is True:
        print('True')
        time.sleep(5)
    else:
        # print('False')
        break

Consider also, this alternative implementation, which checks for any file with the extension:

import os
import time

while True:
    if any(fname.endswith('.path') for fname in os.listdir('.')):
        print('true')
        time.sleep(5)
    else:
        # print('false')
        break

CodePudding user response:

os.listdir returns a list of the items in a directory when it was called. Since this is just a regular list, it will not change automatically. For your desired behavior, put the os.listdir call inside the loop.

while True:
    path = os.listdir('/home/dc4/Downloads/DEB/')
    check = '.part' in path[1] # x[1] == 'grape.part' (this is also a file)
    
    if check:
        print(True)
        time.sleep(5)

CodePudding user response:

You are only getting the path once. So, it does not change when you move the file. Putting the path inside of the loop will fix this.

CodePudding user response:

You must add listdir inside the loop so it can refresh the file contents the script has saved.

Full code:

import os import time

while True:

path = os.listdir(r'/home/dc4/Downloads/DEB/')

check = '.part' in path[1]

if check is True:
    print('True')
    time.sleep(5)
  • Related