[Tutor] how to extract number of files from directory

2005-10-12 Thread Marc Buehler
hi.

i'm new to Python ...

i would like to extract the number of JPG files
from the current directory and use that number
as a parameter in my python script.
i tried:
 a = os.system('ls *JPG | wc -l')
when i do:
 print a
i get '0'.

what am i missing?

marc

---
The apocalyptic vision of a criminally insane charismatic cult leader 

   http://www.marcbuehler.net




__ 
Yahoo! Music Unlimited 
Access over 1 million songs. Try it free.
http://music.yahoo.com/unlimited/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to extract number of files from directory

2005-10-12 Thread Kent Johnson
Marc Buehler wrote:
 i would like to extract the number of JPG files
 from the current directory and use that number
 as a parameter in my python script.
 i tried:
  a = os.system('ls *JPG | wc -l')
 when i do:
  print a
 i get '0'.
 
 what am i missing?

os.system() returns the exit code of the program, not the output. You can run 
an external program and capture the output, but generally you should look for a 
solution within Python before resorting to external programs. In this case, try

import glob
a = len(glob.glob('*.JPG'))

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to extract number of files from directory

2005-10-12 Thread Ertl, John
Marc,

You can do most system type stuff in Python and it makes it much easier.  
import glob
jpgList = glob.glob(*.jpg) # the glob allows you to use wild cards in the
search
jpgCount = len(jpgList)
This gives you a list of all files that end in .jpg.  You can then do a
len(jpgList) to get the number of files
John


 -Original Message-
From:   Marc Buehler [mailto:[EMAIL PROTECTED] 
Sent:   Wednesday, October 12, 2005 11:10 AM
To: tutor@python.org
Subject:[Tutor] how to extract number of files from directory

hi.

i'm new to Python ...

i would like to extract the number of JPG files
from the current directory and use that number
as a parameter in my python script.
i tried:
 a = os.system('ls *JPG | wc -l')
when i do:
 print a
i get '0'.

what am i missing?

marc


---
The apocalyptic vision of a criminally insane charismatic cult leader 

   http://www.marcbuehler.net





__ 
Yahoo! Music Unlimited 
Access over 1 million songs. Try it free.
http://music.yahoo.com/unlimited/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to extract number of files from directory

2005-10-12 Thread Eric Walker
I think os.system returns status of commands

Try this:
w = os.popen('ls -l | wc -l').readlines()

Python Newbie...

On Wednesday 12 October 2005 12:10 pm, Marc Buehler wrote:
 hi.

 i'm new to Python ...

 i would like to extract the number of JPG files
 from the current directory and use that number
 as a parameter in my python script.
 i tried:
  a = os.system('ls *JPG | wc -l')
 when i do:
  print a
 i get '0'.

 what am i missing?

 marc

 ---
 The apocalyptic vision of a criminally insane charismatic cult
 leader

http://www.marcbuehler.net
 ---
-



 __
 Yahoo! Music Unlimited
 Access over 1 million songs. Try it free.
 http://music.yahoo.com/unlimited/
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to extract number of files from directory

2005-10-12 Thread w chun
On 10/12/05, Ertl, John [EMAIL PROTECTED] wrote:
You can do most system type stuff in Python and it makes it much easier.import globjpgList = glob.glob(*.jpg) # the glob allows you to use wild cards in thesearchjpgCount = len(jpgList)This gives you a list of all files that end in .jpg.You can then do a
len(jpgList) to get the number of filesJohnglob seems to be a good idea, but is there a way to do it case-insensitively, i.e., .jpg and .JPG?it may be slower than glob, but i'm finding myself thinking more like...
len([i for i in os.listdir('tmp') if i.lower().endswith('.jpg')])now, anyone with a solution for .jpg, .JPG, .jpeg, *and* .JPEG?-- wesley- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Core Python Programming, Prentice Hall, (c)2006,2001http://corepython.comwesley.j.chun :: wescpy-at-gmail.com
cyberweb.consulting : silicon valley, cahttp://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to extract number of files from directory

2005-10-12 Thread Alan Gauld
 a = os.system('ls *JPG | wc -l')
 when i do:
 print a
 i get '0'.

os.system only returns the exitcode of the command not the output.

Look at the new Subprocess  module and its Popen class. It can 
look intimidating at first but read the examples and you should find 
it easy enough.

Altrernatively try the commands module although subprocess is 
intended to replace it...

However you might find it easier to use the python os tools like listdir 
to do the job directly. 

a = len(os.listdir(mydir, '*JPG')

Should be about right...

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to extract number of files from directory

2005-10-12 Thread Kent Johnson
Alan Gauld wrote:
 a = len(os.listdir(mydir, '*JPG')
 
 Should be about right...

No, os.listdir() only takes one argument, the directory. See the solutions with 
glob.glob().

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to extract number of files from directory

2005-10-12 Thread Alan Gauld
 glob seems to be a good idea, ...

Yep, I got my glob and listdir mixed up earlier...

 it may be slower than glob, but i'm finding myself thinking more like...
 len([i for i in os.listdir('tmp') if i.lower().endswith('.jpg')])

 now, anyone with a solution for .jpg, .JPG, .jpeg, *and* .JPEG?

Sounds like a job for a regex?

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to extract number of files from directory

2005-10-12 Thread Alan Gauld
 a = len(os.listdir(mydir, '*JPG')

 Should be about right...

 No, os.listdir() only takes one argument, the directory. See the solutions 
 with glob.glob().

Oops! Quite right, my mistake. (As is the missing right paren!)

Alan g. 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to extract number of files from directory

2005-10-12 Thread Michael Sparks
On Wednesday 12 October 2005 19:10, Marc Buehler wrote:
 i would like to extract the number of JPG files
 from the current directory and use that number

I've looked through the thread, and the following strikes me as simpler than 
the suggestions so far.

path = . # Current directory, unix at least.
extns = [jpg, jpeg]  # add in any others you like :-)
piccies = [ f for f in os.listdir(path) if f.split(.)[-1].lower() in extns ]
num_files = len(piccies)


What does this do?

os.listdir(path) -- gives you a list of files in that directory.
The condition:
 f.split(.)[-1].lower() in extns

Selects which filenames get put in the resulting list files.

This expression says split the filename on dots '.' , take the last thing
after the last dot, and make it lower. Finally check to see if that extension
is in the list of required extensions. 


I suppose if you prefer a more verbose version of the same thing:

path = . # Current directory, unix at least.
extns = [jpg, jpeg]  # add in any others you like :-)
piccies = []
for filename in os.listdir(path):
extension = f.split(.)[-1]
if extension.lower() in extns:
piccies.append(filename)

num_files = len(piccies)

Regards,


Michael.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor