Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   RE: Moving folders with content (http://www.velocityreviews.com/forums/t952272-re-moving-folders-with-content.html)

09-16-2012 08:02 AM

RE: Moving folders with content
 
Thank you "Nobody" and Hans!

> You may want to use the subprocess module to run 'ditto'. If
> the destination folder does not exist, then ditto will copy MacOS
> specific aspects such as resource forks, ACLs and HFS meta-data.


This looks like a good direction to go. Maybe something like:

>>> import os
>>> import subprocess
>>>
>>> p1 = os.path.expanduser('~/Desktop/IN/Test/')
>>> p2 = os.path.expanduser('~/Desktop/OUT/Test/')
>>>
>>> cmd = 'ditto -vV "' + p1 + '" "' + p2 + '"'
>>>
>>> v = subprocess.check_output(cmd, shell=True)
>>> Copying /Users/jay/Desktop/IN/Test/

copying file ./.DS_Store ...
6148 bytes for ./.DS_Store
copying file ./dude.txt ...
4 bytes for ./dude.txt
copying file ./new.png ...
114469 bytes for ./new.png

Jay

Hans Mulder 09-16-2012 10:40 AM

Re: Moving folders with content
 
On 16/09/12 10:02:09, jyoung79@kc.rr.com wrote:
> Thank you "Nobody" and Hans!


You're welcome!

>> You may want to use the subprocess module to run 'ditto'. If
>> the destination folder does not exist, then ditto will copy MacOS
>> specific aspects such as resource forks, ACLs and HFS meta-data.

>
> This looks like a good direction to go. Maybe something like:
>
>>>> import os
>>>> import subprocess
>>>>
>>>> p1 = os.path.expanduser('~/Desktop/IN/Test/')
>>>> p2 = os.path.expanduser('~/Desktop/OUT/Test/')
>>>>
>>>> cmd = 'ditto -vV "' + p1 + '" "' + p2 + '"'
>>>>
>>>> v = subprocess.check_output(cmd, shell=True)


This looks iffy: it would break if there would be any double
quotes in p1 or p2. You might think that os.path.expanduser
would never expand '~' to something containing a double quote,
but you'd be wrong:

>>> import os
>>> os.environ['HOME'] = 'gotcha!"; rm -rf '
>>> print(os.path.expanduser('~/Desktop/IN/Test/'))

gotcha!"; rm -rf /Desktop/IN/Test/

It's easy and safer to avoid using 'shell=True' option:

cmd = ['ditto', '-vV', p1, p2]
v = subprocess.check_output(cmd, shell=False)

In this case, the safer version also happens to be shorter and
more readable. But you should get into the habit of using
shell=False whenever possible, because it is much easier to
get it right.


Hope this helps,

-- HansM

Nobody 09-16-2012 02:31 PM

Re: Moving folders with content
 
On Sun, 16 Sep 2012 12:40:18 +0200, Hans Mulder wrote:

> But you should get into the habit of using shell=False whenever
> possible, because it is much easier to get it right.


More accurately, you should get into the habit of passing a list as the
first argument, rather than a string.

On Unix-like systems (including Mac OS X), this effectively requires
shell=False. Passing a list with shell=True has behaviour which is
well-defined, but rarely useful (the first element of the list will be
executed as a shell command, the remaining elements will be available via
the shell variables $1, $2, etc within that command).

On Windows, the list is converted to a command string using the same
quoting rules regardless of the value of the shell= parameter. The
difference is that shell=False requires the "executable" to actually be a
binary executable, while shell=True allows it to be some other type of
file (e.g. a batch file, Python script, etc).


Dennis Lee Bieber 09-16-2012 06:53 PM

Re: Moving folders with content
 
On Sun, 16 Sep 2012 15:31:07 +0100, Nobody <nobody@nowhere.com>
declaimed the following in gmane.comp.python.general:


> difference is that shell=False requires the "executable" to actually be a
> binary executable, while shell=True allows it to be some other type of
> file (e.g. a batch file, Python script, etc).


While it is true that "programs" that require an interpreter to be
present won't run directly, the main difference is that "shell=True"
allows for use of the built-ins -- commands like DIR, COPY, DEL...

One can always preface the others with the interpreter program name
(granted, in the case of "BAT" files, that interpreter IS the command
shell, so it if rather perverse to say "run this file in this shell"
while saying "don't use a shell" <G>).


--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/


Dennis Lee Bieber 09-16-2012 08:53 PM

Re: Moving folders with content
 
On Sun, 16 Sep 2012 14:53:02 -0400, Dennis Lee Bieber
<wlfraed@ix.netcom.com> declaimed the following in
gmane.comp.python.general:

> On Sun, 16 Sep 2012 15:31:07 +0100, Nobody <nobody@nowhere.com>
> declaimed the following in gmane.comp.python.general:
>
>
> > difference is that shell=False requires the "executable" to actually be a
> > binary executable, while shell=True allows it to be some other type of
> > file (e.g. a batch file, Python script, etc).

>
> While it is true that "programs" that require an interpreter to be
> present won't run directly, the main difference is that "shell=True"
> allows for use of the built-ins -- commands like DIR, COPY, DEL...
>
> One can always preface the others with the interpreter program name
> (granted, in the case of "BAT" files, that interpreter IS the command
> shell, so it is rather perverse to say "run this file in this shell"
> while saying "don't use a shell" <G>).


I may have over trimmed the quote -- the preceding comment applies
to usage on M$ Windows... (with one typo corrected)
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/



All times are GMT. The time now is 09:06 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.