Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'filename': 0.07; 'next,': 0.07; 'subject:Question': 0.07; 'python': 0.09; 'deletes': 0.09; 'scripting': 0.09; 'thread,': 0.09; 'threads,': 0.09; 'language': 0.14; 'file,': 0.15; '"\\\\"': 0.16; 'backslashes': 0.16; 'literals.': 0.16; 'wrote:': 0.17; 'script.': 0.17; 'file.': 0.20; 'import': 0.21; 'together.': 0.21; 'posted': 0.22; 'nearly': 0.23; 'this:': 0.23; 'linux': 0.24; 'script': 0.24; 'least': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'wrote': 0.26; 'extend': 0.26; 'raw': 0.27; 'strings,': 0.29; 'lists': 0.31; 'file': 0.32; 'print': 0.32; 'comments': 0.33; 'to:addr:python-list': 0.33; 'path': 0.35; 'pm,': 0.35; "won't": 0.35; 'but': 0.36; 'wanted': 0.36; 'should': 0.36; 'two': 0.37; 'rather': 0.37; 'well.': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'skip:o 20': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'notice': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'your': 0.60; 'repeat': 0.62; 'skip:n 10': 0.63; 'forward': 0.66; 'skip:$ 10': 0.66; 'received:74.208': 0.71; 'difference.': 0.84; 'subject:Best': 0.91 Date: Tue, 05 Feb 2013 06:52:23 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Best Practice Question References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:041V9AfPzRgMhRSVVhSjuRoen8NUA097UASF77g0V/4 rGyPwANaZiJGjFtUrx1GKiHFmUMFm0/uBvfQbnW2cQR7BM3WFp DrOX9r1QpnncXJ7xIu9Wb2SoOqfuVTiVNmaTSsu3JmoHu6O98L vGNZi8LqYOUCe7GqTFdZfL5q/N0L/tLYfZXsbKF4UTxBkBbBYw mdgjC99pzDucQb6cLVpPft2FwIhDcfie28Mx9QgC+rigPn2iw3 9GAHG+yMzLOL/qUcH+SVBnHNlLnVrRIuI3Bqac08XBwQEWkhsL pIKERZ/2Ne2Fa/Il2ztr1BrLLzBIlBPESE0C/eNqiU+J+xxXw= = X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360065166 news.xs4all.nl 6900 [2001:888:2000:d::a6]:42490 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38182 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