Re: [Tutor] How can I copy files recursively?

2006-07-11 Thread Alan Gauld
>> Or you could just use mv on the top level folder.
>>
> But I don't want the sub folders to come along with the copy. I'd 
> like to
> grab the mp3 files out of a set of subfolders and place them all 
> into a
> single folder somewhere else.

Ah, sorry, I misunderstood the question.

In that case os.walk and shutil.copy provide a fairly easy solution.
Take a look at the findfile() function in the OS tutor topic.

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] How can I copy files recursively?

2006-07-11 Thread Richard Querin
On 7/11/06, Alan Gauld <[EMAIL PROTECTED]> wrote:
Things which are easy in the shell are usually less easy in Python.In your case a simple cp -r will copy the files and an rm -rf willdelete the originals.Or you could just use mv on the top level folder.
But I don't want the sub folders to come along with the copy. I'd like to grab the mp3 files out of a set of subfolders and place them all into a single folder somewhere else. I'm completely lost when it comes to bash scripting, so I may take Michael P. Reilly's suggestion as a starting point since it's the only one I understand at first glance ;).
I'm really a newbie to python programming so readability and understanding it is first on my list. Efficiency and speed is secondary to me at the moment.Thanks to all for the help.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How can I copy files recursively?

2006-07-11 Thread Alan Gauld
> subfolders. I'd like to write a quick script to move (not copy) all 
> the mp3
> files in those folders into a single destination folder. I was 
> thinking I
> could do it easily from the linux command line (cp -r copies the 
> subfolders
> out as well) but I can't figure out how to do it. Is there an easy 
> way to
> achieve this using Python? I am assuming this would be something 
> Python was
> designed to make easy..

Things which are easy in the shell are usually less easy in Python.
In your case a simple cp -r will copy the files and an rm -rf will
delete the originals.

Or you could just use mv on the top level folder.

However the OS topic in my tutor provides all the bits you need
to write a python script if you really need to.

Alan G. 


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


Re: [Tutor] How can I copy files recursively?

2006-07-11 Thread Michael P. Reilly
On 7/10/06, Richard Querin <[EMAIL PROTECTED]> wrote:
I know this is probably a dumb question:I've got mp3 files that are downloaded (by ipodder) into individual subfolders. I'd like to write a quick script to move (not copy) all the mp3 files in those folders into a single destination folder. I was thinking I could do it easily from the linux command line (cp -r copies the subfolders out as well) but I can't figure out how to do it. Is there an easy way to achieve this using Python? I am assuming this would be something Python was designed to make easy..
Python makes things easy, from a certain point of view.  You have to think that Python is a program language and not a command language (like Bourne shell).Others have a number of solutions that will move your files with 
os.walk and os.path.walk.  They are nice, but sometimes it's better to traverse the tree individually.  Also, you didn't specify if the destination "folder" structure already existed, or if it will need to be created (which cp -r will do).  The following should handle that case for you:
import fnmatch, osdef movetree(srcdir, dstdir, pattern=None):    # dstdir must exist first
    srcnames = os.listdir(srcdir)    for name in srcnames:
    srcfname = os.path.join(srcdir, name)    dstfname = os.path.join(dstdir, name)    if 
os.path.isdir(srcfname):    os.mkdir(dstfname, 00)    movetree(srcfname, dstfname)
    elif pattern is None or fnmatch.fnmatch(name, pattern):    os.rename(srcfname, dstfname)
You could do the same with 
os.walk or os.path.walk, but the recursive nature of the function takes care of itself a bit better, IMO.  -Arcege-- There's so many different worlds,So many different suns.And we have just one world,
But we live in different ones.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How can I copy files recursively?

2006-07-11 Thread Kent Johnson
John Fouhy wrote:
> On 11/07/06, Richard Querin <[EMAIL PROTECTED]> wrote:
>   
>> I know this is probably a dumb question:
>>
>> I've got mp3 files that are downloaded (by ipodder) into individual
>> subfolders. I'd like to write a quick script to move (not copy) all the mp3
>> files in those folders into a single destination folder. I was thinking I
>> could do it easily from the linux command line (cp -r copies the subfolders
>> out as well) but I can't figure out how to do it. Is there an easy way to
>> achieve this using Python? I am assuming this would be something Python was
>> designed to make easy..
>> 
>
> In python, you can use os.rename to move and os.walk to walk your
> directory structure.  Something like:
>
> source = '/ipodder'
> dest = '/foo/bar'
> for root, dirs, files in os.walk(source):
> for f in files:
> os.rename(os.path.join(root, f), os.path.join(dest, f))
This will copy all the files into the dest directory - it doesn't copy 
the folder structure.

I highly recommend Jason Orendorff's path module for any code involving 
walking directories. Something like this:

from path import path
source = path('/ipodder')
dest = '/foo/bar'

for mp3 in source.walkfiles('*.mp3'):
  relpath = source.relpathto(mp3)
  destmp3 = dest / relpath
  os.rename(mp3, destmp3)

http://www.jorendorff.com/articles/python/path/index.html
Kent

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


Re: [Tutor] How can I copy files recursively?

2006-07-10 Thread Danny Yoo
> directory structure.  Something like:
>
> source = '/ipodder'
> dest = '/foo/bar'
> for root, dirs, files in os.walk(source):
>for f in files:
>os.rename(os.path.join(root, f), os.path.join(dest, f))

A helper function here will be useful.  We can simulate something like:

 find . -name '*.mp3'

with:

#
import os, fnmatch

def find_with_glob(dir, pattern):
 """find_with_glob: string string -> listof(string)
 Returns a list of all the files that can be found
 in dir or subdirectories, matching the globbing pattern.

 For example: find_with_glob('.', '*.mp3').
 """
 results = []
 for root, dirs, files in os.walk(dir):
 for f in files:
 if fnmatch.fnmatch(f, pattern):
 results.append(os.path.join(root, f))
 return results
#

The reason this ends up a bit verbose compared to the shell is because the 
shell's 'find' utility is doing a lot of work too.  But if we were to do a 
simple lookup for mp3 files, I agree that using Unix's 'find' would be the 
simplest approach.

One advantage we have in using Python here is generality: we can, with a 
little work, plug in a different kind of filter here: rather than use 
globs, we can use full-fledged regular expressions.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How can I copy files recursively? [Off Topic, AGAIN]

2006-07-10 Thread زياد بن عبدالعزيز الباتلي
This is “Off Topic” regarding the mailing list!

On Mon, 2006-07-10 at 22:57 -0500, Dustin J. Mitchell wrote: (altered by
Ziyad)
> Richard Querin wrote:
> > ...
> > I was thinking I could do it easily from the linux command line
> > (cp -r copies the subfolders out as well) but I can't figure out
> > how to do it.
> > ...
> Python's not always the best with complex manipulations like that.  You
> could use os.path.walk, but here's the same thing in bash:
> 
>  find $IPODDER_DIR -type f -exec mv \{} $DEST_DIR \;
> 
> Dustin
You could use something like this:
find SOURCE_DIR -type f -print0 | xargs -0 -i mv {} DIST_DIR

Where SOURCE_DIR is the top level directory holding the files and
DIST_DIR is the directory you want to move the files to.

Make sure there are no conflict in file names otherwise some files will
be overwritten!

Dustin's version will work perfectly but if there's a lot files it'll
exhaust your system because it will create a new process for each file
it finds (plus another process for the ‘find’ command itself), while the
other version will only create *three* processes only! (One for ‘find’,
another for ‘xargs’ and the third is for *one* ‘mv’ move command.)

(There's another way by using two piped ‘tar’ commands, but that's
overkilling it and the above solution is much more elegant.)

A side note:
I apologise for the list as it seems I'm only good at posting
anything *but* Python related.

Ziyad.

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


Re: [Tutor] How can I copy files recursively?

2006-07-10 Thread John Fouhy
On 11/07/06, Richard Querin <[EMAIL PROTECTED]> wrote:
> I know this is probably a dumb question:
>
> I've got mp3 files that are downloaded (by ipodder) into individual
> subfolders. I'd like to write a quick script to move (not copy) all the mp3
> files in those folders into a single destination folder. I was thinking I
> could do it easily from the linux command line (cp -r copies the subfolders
> out as well) but I can't figure out how to do it. Is there an easy way to
> achieve this using Python? I am assuming this would be something Python was
> designed to make easy..

You could do it from the command line with something like:

$ for f in `find ./ -name *.mp3`; do mv $f /foo/bar/$f; done

(actually, that won't work; you'll need to find a way to convert
"/x/y/z.mp3" to "z.mp3".  I don't have a linux commandline handy to
test.. basename, maybe? You could do it with awk --

$ for f in `find ./ -name *.mp3`; do mv $f /foo/bar/`echo $f | awk -F
'/' '{print $NF}'`; done

but that's pretty hairy and may not work :-/
)

In python, you can use os.rename to move and os.walk to walk your
directory structure.  Something like:

source = '/ipodder'
dest = '/foo/bar'
for root, dirs, files in os.walk(source):
for f in files:
os.rename(os.path.join(root, f), os.path.join(dest, f))

(although I would do a test run first, if I were you, since I often
get os.walk wrong :-) )

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


Re: [Tutor] How can I copy files recursively?

2006-07-10 Thread Dustin J. Mitchell
Richard Querin wrote:
> I know this is probably a dumb question:
> 
> I've got mp3 files that are downloaded (by ipodder) into individual
> subfolders. I'd like to write a quick script to move (not copy) all the
> mp3 files in those folders into a single destination folder. I was
> thinking I could do it easily from the linux command line (cp -r copies
> the subfolders out as well) but I can't figure out how to do it. Is
> there an easy way to achieve this using Python? I am assuming this would
> be something Python was designed to make easy..

Python's not always the best with complex manipulations like that.  You
could use os.path.walk, but here's the same thing in bash:

 find $IPODDER_DIR -type f -exec mv \{} $DEST_DIR \;

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


[Tutor] How can I copy files recursively?

2006-07-10 Thread Richard Querin
I know this is probably a dumb question:I've got mp3 files that are downloaded (by ipodder) into individual subfolders. I'd like to write a quick script to move (not copy) all the mp3 files in those folders into a single destination folder. I was thinking I could do it easily from the linux command line (cp -r copies the subfolders out as well) but I can't figure out how to do it. Is there an easy way to achieve this using Python? I am assuming this would be something Python was designed to make easy..
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor