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


Groups > comp.lang.python > #38179

Re: Opinion on best practice...

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=7419968f6=jeanmichel@sequans.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.047
X-Spam-Evidence '*H*': 0.91; '*S*': 0.00; 'things.': 0.05; 'lines.': 0.07; 'python': 0.09; 'imply': 0.09; 'portable': 0.09; 'scripting': 0.09; 'files.': 0.13; 'language': 0.14; '"\\\\"': 0.16; 'script.': 0.17; 'windows': 0.19; 'module': 0.19; 'import': 0.21; 'minor': 0.22; 'elements': 0.23; 'to:2**1': 0.23; 'linux': 0.24; 'header:In-Reply-To:1': 0.25; 'creating': 0.26; 'correct': 0.28; 'url:mailman': 0.29; 'code': 0.31; 'url:python': 0.32; 'file': 0.32; '-----': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'directory,': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'list': 0.35; 'filter': 0.35; 'path': 0.35; 'but': 0.36; 'url:org': 0.36; 'actions': 0.36; 'should': 0.36; 'thank': 0.36; 'does': 0.37; 'being': 0.37; 'subject:: ': 0.38; 'store': 0.38; 'files': 0.38; 'skip:o 20': 0.38; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'your': 0.60; 'remove': 0.61; 'you.': 0.61; 'received:194': 0.61; 'side': 0.61; 'subject:...': 0.63; 'information': 0.63; 'person,': 0.65; 'disclose': 0.69; 'notice:': 0.71; 'privileged.': 0.72; 'issues:': 0.84; 'do:': 0.91; 'medium.': 0.91
X-IronPort-AV E=Sophos;i="4.84,603,1355094000"; d="scan'208";a="1150015"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Tue, 5 Feb 2013 11:12:58 +0100 (CET)
From Jean-Michel Pichavant <jeanmichel@sequans.com>
To Anthony Correia <akcorreia@gmail.com>, python-list@python.org
In-Reply-To <207d17ce-4bc7-487c-acde-6a7b9f66002b@googlegroups.com>
Subject Re: Opinion on best practice...
MIME-Version 1.0
X-Mailer Zimbra 7.2.0_GA_2669 (ZimbraWebClient - GC7 (Linux)/7.2.0_GA_2669)
Content-Type text/plain; charset="utf-8"
Content-Transfer-Encoding base64
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1360.1360059291.2939.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360059291 news.xs4all.nl 6882 [2001:888:2000:d::a6]:50547
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38179

Show key headers only | View raw


----- 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.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

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

csiph-web