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


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

Re: Opinion on best practice...

Started byJean-Michel Pichavant <jeanmichel@sequans.com>
First post2013-02-05 11:12 +0100
Last post2013-02-05 02:36 -0800
Articles 2 — 2 participants

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


Contents

  Re: Opinion on best practice... Jean-Michel Pichavant <jeanmichel@sequans.com> - 2013-02-05 11:12 +0100
    Re: Opinion on best practice... rusi <rustompmody@gmail.com> - 2013-02-05 02:36 -0800

#38179 — Re: Opinion on best practice...

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2013-02-05 11:12 +0100
SubjectRe: Opinion on best practice...
Message-ID<mailman.1360.1360059291.2939.python-list@python.org>
----- Original Message -----
> I need to pick up a language that would cover the Linux platform.  I
> use Powershell for a scripting language on the Windows side of
> things.  Very simple copy files script.  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 just do:
> 
> $colDir = gci -path "c:\temp2"
> ForEach($file in $colDir)
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

Hi,

Is you powershell code printing the file being removed ? does it remove the file as well ?
Because when you say "I would *just* do" you seem to imply that the same actions can be performed with powershell in very few lines.

Anyway, your python code is correct but contain minor issues:

1/ listdir returns all elements in a directory, including subdirectories, you should filter the list to get only files. os.remove will not remove directories.
2/ activefile = os.path.join(objdir, f) # would be the portable way of joining path elements

objdir = os.path.join('C:', 'temp2')
for f in [f for _f in os.listdir(objdir) if os.path.isfile(_f)]:
    os.remove(os.path.join(objdir, f))

Also note that 'shutil' is a module giving you access to a lot of tools for copying, deleting creating files/dir.

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

[toc] | [next] | [standalone]


#38181

Fromrusi <rustompmody@gmail.com>
Date2013-02-05 02:36 -0800
Message-ID<a141127d-c882-4d4e-aead-96258947fee4@u21g2000vbo.googlegroups.com>
In reply to#38179
> ----- Original Message -----
> > I need to pick up a language that would cover the Linux platform.  I
> > use Powershell for a scripting language on the Windows side of
> > things.  Very simple copy files script.  Is this the best way to do
> > it?

Have you seen/checked  http://pash.sourceforge.net/  ?

[toc] | [prev] | [standalone]


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


csiph-web