Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38160 > unrolled thread

Best Practice Question

Started byAnthony Correia <akcorreia@gmail.com>
First post2013-02-04 20:23 -0800
Last post2013-02-05 06:52 -0500
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Best Practice Question Anthony Correia <akcorreia@gmail.com> - 2013-02-04 20:23 -0800
    Re: Best Practice Question Dave Angel <davea@davea.name> - 2013-02-05 06:52 -0500

#38160 — Best Practice Question

FromAnthony Correia <akcorreia@gmail.com>
Date2013-02-04 20:23 -0800
SubjectBest Practice Question
Message-ID<b93e47a7-9338-4a55-a81f-d6bb595fd8ab@googlegroups.com>
Just started learning Python.  I just wrote a simple copy files script. I use Powershell now as my main scripting language but I wanted to extend into the linux platform as well.  Is this the best way to do it?

import os 

    objdir = ("C:\\temp2") 
    colDir = os.listdir(objdir) 
    for f in colDir: 
        activefile = os.path.join(objdir + "\\" + f) 
        print ("Removing " + activefile + " from " + objdir) 
        os.remove(activefile) 

In Powershell I would do this: 

$colDir = gci -path "c:\temp2"
$objDir = "C:\temp3"
ForEach($file in $colDir){
    #.Fullname lists the directory and filename together.  No need to do a join
    #beforehand.
    Copy-item $file.fullname -destination $objDir
}

[toc] | [next] | [standalone]


#38182

FromDave Angel <davea@davea.name>
Date2013-02-05 06:52 -0500
Message-ID<mailman.1362.1360065166.2939.python-list@python.org>
In reply to#38160
On 02/04/2013 11:23 PM, Anthony Correia wrote:
> Just started learning Python.  I just wrote a simple copy files script. I use Powershell now as my main scripting language but I wanted to extend into the linux platform as well.  Is this the best way to do it?
>
> import os
>
>      objdir = ("C:\\temp2")
>      colDir = os.listdir(objdir)
>      for f in colDir:
>          activefile = os.path.join(objdir + "\\" + f)
>          print ("Removing " + activefile + " from " + objdir)
>          os.remove(activefile)
>
> In Powershell I would do this:
>
> $colDir = gci -path "c:\temp2"
> $objDir = "C:\temp3"
> ForEach($file in $colDir){
>      #.Fullname lists the directory and filename together.  No need to do a join
>      #beforehand.
>      Copy-item $file.fullname -destination $objDir
> }
>

You started two nearly-identical threads, with nearly the same content. 
  I won't repeat the comments already posted in the other thread, but 
notice that your powershell script copies the file, while your Python 
"translation" deletes the file.  Big difference.

Next, you should use raw strings, or at least use the forward slash, 
rather than double backslashes in file path literals.



-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web