> 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
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
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)
> 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-e
>> 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.
>
>> 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 PyMO
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(rootDir
-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
> 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
>> 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 re
> 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
"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
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
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 filesL
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 st
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 =
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 f
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 buf
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
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: u
> "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 ove
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
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/libra
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
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
25 matches
Mail list logo