w3resource

Python: Make file lists from current directory using a wildcard

Python Basic: Exercise-111 with Solution

Write a Python program to make file lists from current directory using a wildcard.

Sample Solution-1:

Python Code:

import glob
file_list = glob.glob('*.*')
print(file_list)
#Specific files
print(glob.glob('*.py'))
print(glob.glob('./[0-9].*'))

Sample Output:

['main.py']
['main.py']
[]

Sample Solution-2:

Python Code:

from pathlib import Path
for path in Path("/").glob("*.*"):
   print(path)

Sample Output:

/.dockerenv 

Python Code Editor:

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to get numbers divisible by fifteen from a list using an anonymous function.
Next: Write a Python program to remove the first item from a specified list.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Try-catch-else construct:

try:
  foo() 
except Exception: 
  print("Exception occured")
else:
  print("Exception didnt occur")
finally:
  print("Always gets here")