Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use python to reproduce bash command 'ls -a' output

Tags:

python

linux

bash

I am new to python and am working on writing bash ls command in python, I am stuck on ls -a option which (according to the manpage):

Include directory entries whose names begin with a dot (`.')

I am aware of os.listdir() but it does not list special entries '.' and '..'

From the docs: os.listdir(path):

Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.

I need help in listing these special entries through python, I would appreciate if someone can help me out here a little.

Thanks all for your patience.

like image 471
APZ Avatar asked Dec 15 '13 02:12

APZ


1 Answers

Just add them manually to os.listdir() result. result = [os.curdir, os.pardir] + os.listdir(path).

Most modern filesystems no longer create the actual hardlinks but all APIs include the names explicitly anyway.

like image 117
Martijn Pieters Avatar answered Oct 22 '22 09:10

Martijn Pieters