views:

109

answers:

4

Like there is a folder say XYZ , whcih contain files with diffrent diffrent format let say .txt file, excel file, .py file etc. i want to display in the output all file name using Python programming

+3  A: 
import glob
glob.glob('XYZ/*')

See the documentation for more

TML
A: 

i want list of all file with number ex xxx.txt, ram.py,raja.xls Output : 1 raja.xls 2 ram.py 3 xxx.txt

+2  A: 

Here is an example that might also help show some of the handy basics of python -- dicts {} , lists [] , little string techniques (split), a module like os, etc.:

bvm@bvm:~/example$ ls
deal.xls    five.xls  france.py  guido.py    make.py      thing.mp3  work2.doc
example.py  four.xls  fun.mp3    letter.doc  thing2.xlsx  what.docx  work45.doc
bvm@bvm:~/example$ python
>>> import os
>>> files = {}
>>> for item in os.listdir('.'):
...     try:
...             files[item.split('.')[1]].append(item)
...     except KeyError:
...             files[item.split('.')[1]] = [item]
... 
>>> files
{'xlsx': ['thing2.xlsx'], 'docx': ['what.docx'], 'doc': ['letter.doc', 
'work45.doc', 'work2.doc'], 'py': ['example.py', 'guido.py', 'make.py', 
'france.py'], 'mp3': ['thing.mp3', 'fun.mp3'], 'xls': ['five.xls',
'deal.xls', 'four.xls']}
>>> files['doc']
['letter.doc', 'work45.doc', 'work2.doc']
>>> files['py']
['example.py', 'guido.py', 'make.py', 'france.py']

For your update question, you might try something like:

>>> for item in enumerate(os.listdir('.')):
...     print item
... 
(0, 'thing.mp3')
(1, 'fun.mp3')
(2, 'example.py')
(3, 'letter.doc')
(4, 'five.xls')
(5, 'guido.py')
(6, 'what.docx')
(7, 'work45.doc')
(8, 'deal.xls')
(9, 'four.xls')
(10, 'make.py')
(11, 'thing2.xlsx')
(12, 'france.py')
(13, 'work2.doc')
>>>
bvmou
+1  A: 
import os

XYZ = '.'

for item in enumerate(sorted(os.listdir(XYZ))):
    print item
mtasic