Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
102 views
in Technique[技术] by (71.8m points)

python - How to see whether a specific filetype such as .xml exists in a folder?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The functions you may want are also in the os module.

import os
dir = '/some/dir'
xml_files = [file for file in os.listdir(dir) if file.lower().endswith('.xml')]
if xml_files:
    ans = input('delete XML files: %s
[y/N] ' % ', '.join(xml_files))
    if ans.lower() in ('y', 'yes'):
        for file in xml_files:
            os.remove(os.path.join(dir, file))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...