Python: List all files in a directory in Python
Python Basic: Exercise-49 with Solution
Write a Python program to list all files in a directory in Python.
Sample Solution:-
import os
def fn(): # 1.Get file names from directory
file_list=os.listdir(r"C:\Users\User\Pictures")
print (file_list)
fn()
======== RESTART: F:/Python_APSC/py-ex-basic-49.py ===========
['47140005.JPG', '55100025.JPG', '57290035.JPG', 'desktop.ini', 'DSC01156.JPG', 'DSC01158.JPG', 'DSC01785.JPG', 'DSC01793.JPG', 'DSC02249.JPG', 'DSC02252.JPG', 'DSC02256.JPG', 'DSC02390.JPG', 'DSC02391.JPG', 'esp8266-12_mod.jpg', 'ESP8266-test-1.png', 'iCloud Photos', 'IMG_4005.jpg', 'IMG_5645.JPG', 'IMG_5772.JPG', 'IMG_6875.JPG', 'Lightroom', 'Pickpik', 'Pickpik.rar', 'P_20170930_210121_vHDR_Auto.jpg', 'P_20170930_210151_vHDR_Auto (1).jpg', 'P_20170930_210208_vHDR_Auto.jpg', 'P_20170930_210229_vHDR_Auto.jpg', 'P_20170930_210248_vHDR_Auto.jpg', 'P_20170930_210318_vHDR_Auto.jpg', 'P_20170930_210328_vHDR_Auto.jpg', 'P_20170930_210426_vHDR_Auto.jpg', 'Thumbs.db']
>>>
沒有留言:
張貼留言