Python: os.listdir alternative/certain extensions
问题 Is it possible to see files with certain extensions with the os.listdir command? I want it to work so it may show only files or folders with .f at the end. I checked the documentation, and found nothing, so don't ask. 回答1: glob is good at this: import glob for f in glob.glob("*.f"): print(f) 回答2: Don't ask what? [s for s in os.listdir() if s.endswith('.f')] If you want to check a list of extensions, you could make the obvious generalization, [s for s in os.listdir() if s.endswith('.f') or s