Converting BASH Test and Regex Code to Python3

This is the place for queries that don't fit in any of the other categories.

Converting BASH Test and Regex Code to Python3

Postby DevynCJohnson » Sat May 25, 2013 2:16 pm

I have this BASH code that I want to convert to Python3.

Code: Select all
if [ -r ./extensions/$DOCKNAME*.{nce,Nce,NCE,nCe} ]; then ./extensions/$DOCKNAME*.{nce,Nce,NCE,nCe}; fi


The two parts that I am not sure how to change is the "-r" test construct (test if file is executable) and "{nce,Nce,NCE,nCe}" (the file may end in any of these extensions). How do I write this is Python3?
Ubuntu 13.04 | Python3.3
User avatar
DevynCJohnson
 
Posts: 21
Joined: Sat May 25, 2013 1:53 pm

Re: Converting BASH Test and Regex Code to Python3

Postby stranac » Sat May 25, 2013 3:27 pm

You can use os.access() with os.X_OK for the -r part.

For the {nce,Nce,NCE,nCe} part, I think you'll have to do some manual filtering, since glob doesn't support expressions like that.
You can use str.endswith() to make it a bit simpler.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: Converting BASH Test and Regex Code to Python3

Postby DevynCJohnson » Sat May 25, 2013 7:20 pm

Thank you; that helps.
Ubuntu 13.04 | Python3.3
User avatar
DevynCJohnson
 
Posts: 21
Joined: Sat May 25, 2013 1:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: W3C [Linkcheck] and 4 guests