The current directory contains lots of *.pyc file. List all of file has extension pyc.

Hint:

  • function os.listdir(folder_path) return all file in folder path
  • solution can write in one line

In [6]:
import os
os.listdir('.')


Out[6]:
['.DS_Store',
 '.git',
 '.gitignore',
 '.ipynb_checkpoints',
 '0.0_Introduction.ipynb',
 '0.0_Introduction.slides.html',
 '1.0_Control_Flows.html',
 '1.0_Control_Flows.ipynb',
 '1.0_Control_Flows.slides.html',
 '2.0_Functions.ipynb',
 '2.0_Functions.slides.html',
 '3.0_Data_structures.ipynb',
 '3.0_Data_structures.slides.html',
 '4.0_Modules.ipynb',
 '4.0_Modules.slides.html',
 '5.0_Practices.ipynb',
 'assets',
 'examples',
 'LICENSE',
 'main.py',
 'main.pyc',
 'module_a.py',
 'module_a.pyc',
 'module_b.py',
 'module_b.pyc',
 'README.md']

In [5]:
import os

[fname for fname in os.listdir('.') if fname.endswith('.pyc')]


Out[5]:
['main.pyc', 'module_a.pyc', 'module_b.pyc']

Remove all *.pyc file

Hint:

  • Using os.remove(file_path) function

In [ ]:
[os.remove(fname) for fname in os.listdir('.') if fname.endswith('.pyc')]

In [ ]: