N34
N34

Reputation: 2233

egrep to list devices in /dev directory

I am using following command in my Python code to list devices available in /dev directory:

 devices = ["/dev/" + x for x in os.popen("ls /dev/ | egrep -i 'ttyUSB|ttyS'").read().strip().split('\n')]

How can I modify it to find only ttyS0 and ttyS1? Currently it returns all ttyS s :

ttyS0,ttyS1, ttyS11, ttyS12, ...

Upvotes: 1

Views: 77

Answers (1)

Steve Vinoski
Steve Vinoski

Reputation: 20014

Change the pattern to the following:

 devices = ["/dev/" + x for x in os.popen("ls /dev/ | egrep -i 'ttyUSB|ttyS[01]$'").read().strip().split('\n')]

Upvotes: 1

Related Questions