Best Practice Question

Discussion in 'Python' started by Anthony Correia, Feb 5, 2013.

  1. 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
    }
    Anthony Correia, Feb 5, 2013
    #1
    1. Advertising

  2. Anthony Correia

    Dave Angel Guest

    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
    Dave Angel, Feb 5, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bryan Ax
    Replies:
    0
    Views:
    602
    Bryan Ax
    Jun 26, 2003
  2. Prince

    Best Practice - Question

    Prince, Jan 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    337
    Kevin Spencer
    Jan 19, 2004
  3. Josh

    Best Practice Question

    Josh, Jun 23, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    331
    Mike Labosh
    Jun 23, 2005
  4. Jason MacKenzie

    Best Practice Question

    Jason MacKenzie, Nov 21, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    389
    Jason Kester
    Nov 22, 2005
  5. oldyork90
    Replies:
    1
    Views:
    154
    Jeremy J Starcher
    Sep 10, 2008
Loading...

Share This Page