Quantcast
Viewing all articles
Browse latest Browse all 14040

FileNotFoundError: [WinError 2] The system cannot find the file specified: find extensions in os.listdir

Trying to delete all but one file in a given directory. Looked at other related posts but my question seems specific to getting names from os.listdir and needing full path with extensions to use os.remove:

# delete files from the save path but preserve the zip fileif os.path.isfile(zip_path):    for clean_up in os.listdir(data_path):        if not clean_up.endswith(tStamp+'.zip'):            os.remove(clean_up)

Gives this error:

Line 5:     os.remove(clean_up)    FileNotFoundError: [WinError 2] The system cannot find the file specified: 'firstFileInListName'

I think this is because os.listdir is not capturing the file extension of each file (printed os.listdir(data_path) and only got names of the files without extensions)

What can I do to delete all files from the data_path except for the one that ends with tStamp+'.zip' ?


Viewing all articles
Browse latest Browse all 14040

Trending Articles