LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-06-2008, 01:33 PM   #1
dave201
LQ Newbie
 
Registered: Oct 2005
Location: Camelot
Distribution: cent, RH core, slax, slackware... *buntu...
Posts: 28

Rep: Reputation: 15
python regexp?


greetings and such.

im trying to figure this regex thing out in python but am having a bit of trouble. down below on the os.path line, im trying to find any files in that directory that end in ".lock"

The below code doesn't work, anyone know how to get this done?

Code:
import os
import re

if os.path.exists('/home/dave/code/*.lock) == True:
        print "stuff is locked"
else:
        print "not locked"
 
Old 05-06-2008, 02:49 PM   #2
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
glob would work better, but if you really wanted to go the regex route, you could do something like this:

Code:
import os, re
# Create a regular expression object to match against
lock_re = re.compile('([.]*.lock)')

# grab a list of filenames in /home/dave/code
for filename in os.listdir('/home/dave/code'):
    # check to see if the filename matches the regex
    # this step could also be done like such:
    # if filename.endswith('.lock'):
    lockMatch = lock_re.search(filename)
    if lockMatch is not None:
        print "stuff is locked"
        break
The same with glob would look something like this:

Code:
import glob

# check the current working directory...
# can use os.chdir to check a dir other than the cwd
filelist = glob.glob('*.lock')

if len(filelist) > 0:
    print "stuff is locked"
else:
    print "not locked"
glob would also be a better solution since you could then iterate over the filelist to see what exactly is locked.

Last edited by rocket357; 05-06-2008 at 02:52 PM.
 
Old 05-06-2008, 02:50 PM   #3
dave201
LQ Newbie
 
Registered: Oct 2005
Location: Camelot
Distribution: cent, RH core, slax, slackware... *buntu...
Posts: 28

Original Poster
Rep: Reputation: 15
hero award for rocket357 thanks!
 
Old 05-06-2008, 03:05 PM   #4
Ome_Roel
LQ Newbie
 
Registered: Sep 2007
Posts: 9

Rep: Reputation: 1
Goodevening, i found this on the pythonDOTorg site:
Code:
import fnmatch
import os

for file in os.listdir('.'):
    if fnmatch.fnmatch(file, '*.txt'):
        print file
Yours kindly Roel

Last edited by Ome_Roel; 05-06-2008 at 03:08 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
regexp help cliff76 Linux - Newbie 3 03-07-2008 02:15 PM
Help with regexp anupamsr Linux - Software 4 03-05-2008 06:43 AM
regexp ovince Programming 8 06-17-2007 10:54 AM
regexp stomach Linux - Software 7 02-15-2006 06:33 PM
little help for regexp EmOuBi Linux - Newbie 6 08-06-2005 02:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration