Re: [Tutor] Move all files to top-level directory

2010-04-13 Thread Dotan Cohen
> Actual the first item in the tuple (returned by os.walk) is singular (a
> string), so I might call it rootDir.  Only the other two needed to be
> changed to plural to indicate that they were lists.

I did discover this, too. I wanted to finish with the code at hand
before I started experimenting with things.


> Note that it's not just race conditions that can cause collisions.  You
> might have the same name in two distinct subdirectories, so they'll end up
> in the same place.  Which one wins depends on the OS you're running, I
> believe.
>

The code checks for that, and assigns a different filename if the name
is already taken. See the renameNumber variable.

Thank you very much for your help.


-- 
Dotan Cohen

http://bido.com
http://what-is-what.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-13 Thread Dave Angel

Dotan Cohen wrote:

Here is the revised version:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
currentDir = os.getcwd()
i = 1
filesList = os.walk(currentDir)
for rootDirs, folders, files in filesList:
  
Actual the first item in the tuple (returned by os.walk) is singular (a 
string), so I might call it rootDir.  Only the other two needed to be 
changed to plural to indicate that they were lists.

for f in files:
if (rootDirs!=currentDir):
toMove  = os.path.join(rootDirs, f)
print "--- "+str(i)
print toMove
newFilename = os.path.join(currentDir,f)
renameNumber = 1
while(os.path.exists(newFilename)):
print "- "+newFilename
newFilename = os.path.join(currentDir,f)+"_"+str(renameNumber)
renameNumber = renameNumber+1
print newFilename
i=i+1
os.rename(toMove, newFilename)

Now, features to add:
1) Remove empty directories. I think that os.removedirs will work here.
2) Prevent race conditions by performing the filename check during
write. For that I need to find a function that fails to write when the
file exists.
3) Confirmation button to prevent accidental runs in $HOME for
instance. Maybe add some other sanity checks. If anybody is still
reading, I would love to know what sanity checks would be wise to
perform.

Again, thanks to all who have helped.


  
Note that it's not just race conditions that can cause collisions.  You 
might have the same name in two distinct subdirectories, so they'll end 
up in the same place.  Which one wins depends on the OS you're running, 
I believe.


DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-13 Thread Dotan Cohen
Here is the revised version:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
currentDir = os.getcwd()
i = 1
filesList = os.walk(currentDir)
for rootDirs, folders, files in filesList:
for f in files:
if (rootDirs!=currentDir):
toMove  = os.path.join(rootDirs, f)
print "--- "+str(i)
print toMove
newFilename = os.path.join(currentDir,f)
renameNumber = 1
while(os.path.exists(newFilename)):
print "- "+newFilename
newFilename = os.path.join(currentDir,f)+"_"+str(renameNumber)
renameNumber = renameNumber+1
print newFilename
i=i+1
os.rename(toMove, newFilename)

Now, features to add:
1) Remove empty directories. I think that os.removedirs will work here.
2) Prevent race conditions by performing the filename check during
write. For that I need to find a function that fails to write when the
file exists.
3) Confirmation button to prevent accidental runs in $HOME for
instance. Maybe add some other sanity checks. If anybody is still
reading, I would love to know what sanity checks would be wise to
perform.

Again, thanks to all who have helped.


-- 
Dotan Cohen

http://bido.com
http://what-is-what.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-13 Thread Dotan Cohen
> I would add a few different features to this 'find' to make it a bit
> more resistant to failure, although this sort of solution is always
> subject to the "somebody else is toying with my filesystem race
> condition".
>
>  find "$srcdir" -depth -type f -print0 \
>    | xargs --null --no-run-if-empty -- \
>      mv --target-directory "$dstdir" --
>
> The --target-directory option is only available in GNU mv (and cp),
> I believe.
>

I am trying to get into Python now, but I will go over that at a later
time to improve my shell scripting skills. Thanks. I do appreciate the
instruction.


> I'll second the recommendation for 'shutil', although you can have
> some fun playing with recursion by building your tree flattener
> using os.walk(), os.path.split() and os.path.join().
>

For certain values of "fun"! Actually, I did enjoy this exercise. I
learned a lot, and found some great resources. Python really is a fun
language.

-- 
Dotan Cohen

http://bido.com
http://what-is-what.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-13 Thread Dotan Cohen
>> I use this one-liner for moving photos nested a single folder deep
>> into the top-level folder:
>> find * -name "*.jpg" | awk  -F/ '{print "mv "$0,$1"-"$2}' | sh
>
> You could miss out the awk and use the exec option of find...
>
> Or miss out the shell and use the system() function of awk.
>

The truth is that is not really my code, I modified it from something
that I found. I really want to code my own, and to understand what I
am doing. I'd rather use a real language for it, not a scripting
language as I want the knowledge to apply as generally as possible.


> You won't easily get a one liner to do the same in Python but you
> can do the same things using the glob, subprocess, shutil, os and path
> modules. In particular look at the os.walk and the shutil.move functions.
>

I am not looking for a one-liner. But thanks for clarifying.


> In addition to the module documentation you will find examples and
> description of both in the Using the OS topic of my tutorial.
>

I will read that. I did see your site once, I even bookmarked it. I
will go over it thoroughly! I actually have some comments about the
site, would you like them in private mail if you are interested?

Thanks!


-- 
Dotan Cohen

http://bido.com
http://what-is-what.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-13 Thread Dotan Cohen
>> When combining directory paths, it's generally safer to use
>>
>> os.path.join()
>
> As KDE/Dolphin runs on windows this is even more important as it will
> sort out the directory separator (/ vs \) for you.
>

Added, thanks!

> Some added reading on os.path can be found on Doug's excellent PyMOTW
> [1]. Also check out the glob module [2].
>
> Greets
> Sander
>
> [1] http://blog.doughellmann.com/2008/01/pymotw-ospath.html
> [2] http://blog.doughellmann.com/2007/07/pymotw-glob.html
>

Oh, that is a great site! Any others that I should know about? Being
new to the Python community, I am as of yet unfamiliar with the
standard resources.

Thanks!

-- 
Dotan Cohen

http://bido.com
http://what-is-what.com

Please CC me if you want to be sure that I read your message. I do not
read all list mail.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-13 Thread Dotan Cohen
All right, I've got it! This script will move all files of
subdirectories into cwd.

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
currentDir = os.getcwd()
filesList = os.walk(currentDir)
for rootDirs, folders, files in filesList:
for f in files:
toMove  = os.path.join(rootDirs, f)
newFilename = os.path.join(currentDir,f)
os.rename(toMove, newFilename)

Now, features to add:
1) Smart rename: don't clobber existing files
2) Remove empty directories
3) Check that it works with spaces in filenames and directories
4) Check that it works with non-ascii UTF-8 characters in filenames
and directories
5) Confirmation button to prevent accidental runs in $HOME for instance.

Thank you to everyone who helped!

-- 
Dotan Cohen

http://bido.com
http://what-is-what.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-13 Thread Martin A. Brown
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Hello,

 : I use this one-liner for moving photos nested a single folder deep
 : into the top-level folder:
 :
 : find * -name "*.jpg" | awk  -F/ '{print "mv "$0,$1"-"$2}' | sh

I would add a few different features to this 'find' to make it a bit 
more resistant to failure, although this sort of solution is always 
subject to the "somebody else is toying with my filesystem race 
condition".

  find "$srcdir" -depth -type f -print0 \
| xargs --null --no-run-if-empty -- \
  mv --target-directory "$dstdir" --

The --target-directory option is only available in GNU mv (and cp), 
I believe.

I'll second the recommendation for 'shutil', although you can have 
some fun playing with recursion by building your tree flattener 
using os.walk(), os.path.split() and os.path.join().

- -Martin

- -- 
Martin A. Brown
http://linux-ip.net/
-BEGIN PGP SIGNATURE-
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: pgf-0.72 (http://linux-ip.net/sw/pine-gpg-filter/)

iD8DBQFLxB5oHEoZD1iZ+YcRAvPUAKCmXcIhKVTUDx4IexWnAvnl64uQNACeOFf/
3tsR/sXGVe944dUMhxkPYkk=
=x0EG
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-13 Thread Dotan Cohen
> Why is the print below commented out?
>>
>>    for f in file:
>>        toMove =oot + "/" + f
>>        #print toMove
>>        os.rename(toMove, currentDir)
>>

I was testing, and it was not longer needed. It was returning what I
expected. Note that print can only be used to test that the value
matches what the dev expects, not to test that the value matches what
the function wants as we will soon see in the case of currentDir.


> Have you looked at the value of "currentDir" ? Is it in a form that's
> acceptible to os.rename() ?

No, but I though that it was. I had assumed that os.rename would
automatically add the filename as the unix command mv does. Yes, I
know what assumptions make out of me!


> And how about toMove? Perhaps it has two slashes
> in a row in it.

No, toMove was fine.


> When combining directory paths, it's generally safer to use
>
> os.path.join()
>

Thanks!


> Next, you make no check whether "root" is the same as "currentDir". So if
> there are any files already in the top-level directory, you're trying to
> rename them to themselves.
>

I have no problem with that. It is unnecessary, but not harmful at
this stage. But thank you for mentioning it, I will get to that.


> I would also point out that your variable names are very confusing. "file"
> is a list of files, so why isn't it plural? Likewise "folders."
>

Obfuscation! Just kidding, I did not realize that I would be
interacting with "file" as a list of files and not as an individual
file at the time I wrote that. I will revise the variable names, that
is good practice.

Thank you for your patience and advice. I am really enjoying Python
now that I am starting to get the hang of it. I do wish that the docs
had more usage examples but the help on this list and the volume of
information available through google mostly negates that. Thank you
very much.


-- 
Dotan Cohen

http://bido.com
http://what-is-what.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-13 Thread Dotan Cohen
>> I see, thanks. So I was sending it four values apparently. I did not
>> understand the error message.
>
> No, you're sending it two values:  a tuple, and a string.  It wants two
> strings.  Thus the error. If you had sent it four values, you'd have gotten
> a different error.

I see. For some reason I was thinking that the tuple was just three
strings. I've slept now and it is clear to me that this is not the
case!


>>> It might be wise to only have this module print what it would do
>>> instead of doing the actual move/rename so you can work out the bugs
>>> first before it destroys your data.
>>
>> I am testing on fake data, naturally.
>
> Is your entire file system fake?  Perhaps you're running in a VM, and don't
> mind trashing it.
>
> While debugging, you're much better off using prints than really moving
> files around.  You might be amazed how much damage a couple of minor bugs
> could cause.
>

I know that you are right. However, using prints would not have helped
me in my current case of not fully understanding the os.rename
function. I should probably develop as a different user, though, to
prevent "incidents". Thanks for the advice.

-- 
Dotan Cohen

http://bido.com
http://what-is-what.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-13 Thread Dotan Cohen
> from the docs:
> os.rename(src, dst)¶Rename the file or directory src to dst. If dst is a
> directory, OSError will be raised.

I did read that, thank you. That is why I asked how to override, as I
understood that Python was functioning exactly as intended.


> It seems what you wan to
> do is os.rename(toMove, currentDir+f)
>

Well, actually, that would be currentDir+"/"+f but you are correct! I
was thinking too much in terms of the unix mv command, which adds that
automatically. Thank you!


-- 
Dotan Cohen

http://bido.com
http://what-is-what.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Alan Gauld


"Dotan Cohen"  wrote


I use this one-liner for moving photos nested a single folder deep
into the top-level folder:
find * -name "*.jpg" | awk  -F/ '{print "mv "$0,$1"-"$2}' | sh


You could miss out the awk and use the exec option of find...

Or miss out the shell and use the system() function of awk.


I need. I have googled file handling in Python but I simply cannot get
something to replicate the current functionality of that lovely
one-liner. What fine manual should I be reading? I am not asking for
code, rather just a link to the right documentation.


You won't easily get a one liner to do the same in Python but you
can do the same things using the glob, subprocess, shutil, os and 
path modules. In particular look at the os.walk and the shutil.move 
functions.


In addition to the module documentation you will find examples and 
description of both in the Using the OS topic of my tutorial.


HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Sander Sweers
On 12 April 2010 22:13, Dave Angel  wrote:
> When combining directory paths, it's generally safer to use
>
> os.path.join()

As KDE/Dolphin runs on windows this is even more important as it will
sort out the directory separator (/ vs \) for you.

Some added reading on os.path can be found on Doug's excellent PyMOTW
[1]. Also check out the glob module [2].

Greets
Sander

[1] http://blog.doughellmann.com/2008/01/pymotw-ospath.html
[2] http://blog.doughellmann.com/2007/07/pymotw-glob.html
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Dave Angel



Dotan Cohen wrote:

All right, I have gotten quite a bit closer, but Python is now
complaining about the directory not being empty:

✈dcl:test$ cat moveUp.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
currentDir =s.getcwd()

filesList =s.walk(currentDir)
for root, folder, file in filesList:
  

Why is the print below commented out?

for f in file:
toMove =oot + "/" + f
#print toMove
os.rename(toMove, currentDir)

✈dcl:test$ ./moveUp.py
Traceback (most recent call last):
  File "./moveUp.py", line 11, in 
os.rename(toMove, currentDir)
OSError: [Errno 39] Directory not empty


I am aware that the directory is not empty, nor should it be! How can
I override this?

Thanks!

  
Have you looked at the value of "currentDir" ? Is it in a form that's 
acceptible to os.rename() ? And how about toMove? Perhaps it has two 
slashes in a row in it. When combining directory paths, it's generally 
safer to use


os.path.join()

Next, you make no check whether "root" is the same as "currentDir". So 
if there are any files already in the top-level directory, you're trying 
to rename them to themselves.


I would also point out that your variable names are very confusing. 
"file" is a list of files, so why isn't it plural? Likewise "folders."


DaveA


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Dave Angel

Dotan Cohen wrote:

On 12 April 2010 20:12, Sander Sweers  wrote:
  

On 12 April 2010 18:28, Dotan Cohen  wrote:


However, it fails like this:
$ ./moveUp.py
Traceback (most recent call last):
 File "./moveUp.py", line 8, in 
   os.rename(f, currentDir)
TypeError: coercing to Unicode: need string or buffer, tuple found
  

os.rename needs the oldname and the new name of the file. os.walk
returns a tuple with 3 values and it errors out.




I see, thanks. So I was sending it four values apparently. I did not
understand the error message.

  
No, you're sending it two values:  a tuple, and a string.  It wants two 
strings.  Thus the error. If you had sent it four values, you'd have 
gotten a different error.


Actually, I will add a check that cwd !=HOME || $HOME/.bin as those
are the only likely places it might run by accident. Or maybe I'll
wrap it in Qt and add a confirm button.


  

os.walk returns you a tuple with the following values:
(the root folder, the folders in the root, the files in the root folder).

You can use tuple unpacking to split each one in separate values for
your loop. Like:

for root, folder, files in os.walk('your path):
  #do stuff




I did see that while googling, but did not understand it. Nice!


  

Judging from your next message, you still don't understand it.

It might be wise to only have this module print what it would do
instead of doing the actual move/rename so you can work out the bugs
first before it destroys your data.




I am testing on fake data, naturally.

  
Is your entire file system fake?  Perhaps you're running in a VM, and 
don't mind trashing it.


While debugging, you're much better off using prints than really moving 
files around.  You might be amazed how much damage a couple of minor 
bugs could cause.


DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Wayne Werner
On Mon, Apr 12, 2010 at 1:21 PM, Dotan Cohen  wrote:

> All right, I have gotten quite a bit closer, but Python is now
> complaining about the directory not being empty:
>
> ✈dcl:test$ cat moveUp.py
> #!/usr/bin/python
> # -*- coding: utf-8 -*-
> import os
> currentDir = os.getcwd()
>
> filesList = os.walk(currentDir)
> for root, folder, file in filesList:
>for f in file:
>toMove = root + "/" + f
>#print toMove
>os.rename(toMove, currentDir)
>
> ✈dcl:test$ ./moveUp.py
> Traceback (most recent call last):
>   File "./moveUp.py", line 11, in 
>os.rename(toMove, currentDir)
> OSError: [Errno 39] Directory not empty
>
>
> I am aware that the directory is not empty, nor should it be! How can
> I override this?
>

from the docs:
os.rename(*src*, *dst*)¶
Rename
the file or directory *src* to *dst*. If *dst* is a directory,
OSErrorwill
be raised. On Unix, if
*dst* exists and is a file, it will be replaced silently if the user has
permission. The operation may fail on some Unix flavors if *src* and
*dst*are on different filesystems. If successful, the renaming will be
an atomic
operation (this is a POSIX requirement). On Windows, if *dst* already
exists, 
OSErrorwill
be raised even if it is a file; there may be no way to implement an
atomic rename when *dst* names an existing file. Availability: Unix,
Windows.It seems what you wan to do is os.rename(toMove, currentDir+f)

HTH,
Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Dotan Cohen
All right, I have gotten quite a bit closer, but Python is now
complaining about the directory not being empty:

✈dcl:test$ cat moveUp.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
currentDir = os.getcwd()

filesList = os.walk(currentDir)
for root, folder, file in filesList:
for f in file:
toMove = root + "/" + f
#print toMove
os.rename(toMove, currentDir)

✈dcl:test$ ./moveUp.py
Traceback (most recent call last):
  File "./moveUp.py", line 11, in 
os.rename(toMove, currentDir)
OSError: [Errno 39] Directory not empty


I am aware that the directory is not empty, nor should it be! How can
I override this?

Thanks!

-- 
Dotan Cohen

http://bido.com
http://what-is-what.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Dotan Cohen
On 12 April 2010 20:12, Sander Sweers  wrote:
> On 12 April 2010 18:28, Dotan Cohen  wrote:
>> However, it fails like this:
>> $ ./moveUp.py
>> Traceback (most recent call last):
>>  File "./moveUp.py", line 8, in 
>>    os.rename(f, currentDir)
>> TypeError: coercing to Unicode: need string or buffer, tuple found
>
> os.rename needs the oldname and the new name of the file. os.walk
> returns a tuple with 3 values and it errors out.
>

I see, thanks. So I was sending it four values apparently. I did not
understand the error message.

> Also os.getcwd returns the working dir so if you run it in the wrong
> folder you will end up with a mess. In idle on my windows machine at
> work this is what is gives me.
>
 os.getcwd()
> 'C:\\Python26'
>
> So it is better to give the program the path you want it to look in
> rather then relying on os.getcwd().
>

I intend to use this in a Dolphin (KDE file manager) service menu
only. But I will try to be careful about that in the future. Running
the script in $HOME might be interesting!

Actually, I will add a check that cwd != $HOME || $HOME/.bin as those
are the only likely places it might run by accident. Or maybe I'll
wrap it in Qt and add a confirm button.


> os.walk returns you a tuple with the following values:
> (the root folder, the folders in the root, the files in the root folder).
>
> You can use tuple unpacking to split each one in separate values for
> your loop. Like:
>
> for root, folder, files in os.walk('your path):
>   #do stuff
>

I did see that while googling, but did not understand it. Nice!


> It might be wise to only have this module print what it would do
> instead of doing the actual move/rename so you can work out the bugs
> first before it destroys your data.
>

I am testing on fake data, naturally.

Thanks!


-- 
Dotan Cohen

http://bido.com
http://what-is-what.com

Please CC me if you want to be sure that I read your message. I do not
read all list mail.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Sander Sweers
On 12 April 2010 18:28, Dotan Cohen  wrote:
> However, it fails like this:
> $ ./moveUp.py
> Traceback (most recent call last):
>  File "./moveUp.py", line 8, in 
>    os.rename(f, currentDir)
> TypeError: coercing to Unicode: need string or buffer, tuple found

os.rename needs the oldname and the new name of the file. os.walk
returns a tuple with 3 values and it errors out.

Also os.getcwd returns the working dir so if you run it in the wrong
folder you will end up with a mess. In idle on my windows machine at
work this is what is gives me.

>>> os.getcwd()
'C:\\Python26'

So it is better to give the program the path you want it to look in
rather then relying on os.getcwd().

os.walk returns you a tuple with the following values:
(the root folder, the folders in the root, the files in the root folder).

You can use tuple unpacking to split each one in separate values for
your loop. Like:

for root, folder, files in os.walk('your path):
   #do stuff

It might be wise to only have this module print what it would do
instead of doing the actual move/rename so you can work out the bugs
first before it destroys your data.

Hope this helps.
Greets
Sander
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Dotan Cohen
I'm really stuck here. I need move all files in subdirectories of cwd
to cwd. So that, for instance, if we are in ~/photos then this file:
~/photos/a/b/file with space.jpg
...will move to this location:
~/photos/file with space.jpg

This is what I've come up with:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
currentDir = os.getcwd()

files = os.walk(currentDir)
for f in files:
os.rename(f, currentDir)


However, it fails like this:
$ ./moveUp.py
Traceback (most recent call last):
  File "./moveUp.py", line 8, in 
os.rename(f, currentDir)
TypeError: coercing to Unicode: need string or buffer, tuple found

I can't google my way out of this one! The filenames on my test setup
are currently ascii, but this does need to be unicode-compatible as
the real filenames will have unicode non-ascii characters and even
spaces in the filenames!

What should I be reading now?

Thanks!


-- 
Dotan Cohen

http://bido.com
http://what-is-what.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Dotan Cohen
> "Lovely"??? What on earth does it do? It's worse than Perl code!!!
> *half a wink*
>

Like a good wife, it does what I need even if it is not pretty on the
eyes. _That_ is lovely!
(I can get away with that, I'm married to a redhead.)


> See the shell utilities module:
>
> import shutil
>

It overwrites files. I am playing with os.rename instead as the two
directories will always be on the same file system.

One thing that I am stuck with is os.walk for getting all the
subfolders' files. Now that at least I have discovered the _names_ of
the functions that I need I can google my out of this! I will write
back when I have a working solution, for the sake of the archives.
Thanks!


-- 
Dotan Cohen

http://bido.com
http://what-is-what.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Steven D'Aprano
On Tue, 13 Apr 2010 12:11:30 am Dotan Cohen wrote:
> I use this one-liner for moving photos nested a single folder deep
> into the top-level folder:
> find * -name "*.jpg" | awk  -F/ '{print "mv "$0,$1"-"$2}' | sh
>
> I would like to expand this into an application that handles
> arbitrary nesting and smart rename, so I figure that Python is the
> language that I need. I have googled file handling in Python but I
> simply cannot get something to replicate the current functionality of
> that lovely one-liner. 

"Lovely"??? What on earth does it do? It's worse than Perl code!!!
*half a wink*


> What fine manual should I be reading? I am not 
> asking for code, rather just a link to the right documentation.

See the shell utilities module:

import shutil




-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Dotan Cohen
On 12 April 2010 17:23, Serdar Tumgoren  wrote:
>  What fine manual should I be reading? I am not asking for
>>
>> code, rather just a link to the right documentation.
>
> You'll definitely want to explore the os module, part of Python's built-in
> standard library.
>
> http://docs.python.org/library/os.html
> http://docs.python.org/library/os.html#files-and-directories
>

Thanks, going through there now...

I found these relevant functions:
os.listdir
os.rename
os.removedirs (this looks great, as it is recursive and nondestructive)


> There are a bunch of recipes on ActiveState and elsewhere online that can
> demonstrate how the os methods are used to work with files and directories.
> Some googling for the various method names (eg. os.mkdir ) should turn them
> up.
>

ActiveState looks like a great resource. Thanks!

-- 
Dotan Cohen

http://bido.com
http://what-is-what.com

Please CC me if you want to be sure that I read your message. I do not
read all list mail.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Serdar Tumgoren
 What fine manual should I be reading? I am not asking for

> code, rather just a link to the right documentation.
>

You'll definitely want to explore the os module, part of Python's built-in
standard library.

http://docs.python.org/library/os.html
http://docs.python.org/library/os.html#files-and-directories

There are a bunch of recipes on ActiveState and elsewhere online that can
demonstrate how the os methods are used to work with files and directories.
Some googling for the various method names (eg. os.mkdir ) should turn them
up.

Best of luck,

Serdar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Move all files to top-level directory

2010-04-12 Thread Dotan Cohen
I use this one-liner for moving photos nested a single folder deep
into the top-level folder:
find * -name "*.jpg" | awk  -F/ '{print "mv "$0,$1"-"$2}' | sh

I would like to expand this into an application that handles arbitrary
nesting and smart rename, so I figure that Python is the language that
I need. I have googled file handling in Python but I simply cannot get
something to replicate the current functionality of that lovely
one-liner. What fine manual should I be reading? I am not asking for
code, rather just a link to the right documentation.

Thanks.

-- 
Dotan Cohen

http://bido.com
http://what-is-what.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor