Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'arguments': 0.05; 'executing': 0.05; 'behavior.': 0.07; 'python': 0.07; 'command.': 0.09; 'exception.': 0.09; 'executed': 0.09; 'executes': 0.09; 'from:addr:python': 0.09; 'ioerror:': 0.09; 'overwrite': 0.09; 'path.': 0.09; 'linux': 0.11; 'wrote:': 0.14; "'fatal:": 0.16; '(windows': 0.16; 'destination.': 0.16; 'dst': 0.16; 'err:': 0.16; 'failed.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'overwriting': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'reply-to:addr:python-list': 0.16; 'subject:command': 0.16; 'thrown': 0.16; 'great.': 0.16; 'fine': 0.18; 'command': 0.19; 'code,': 0.20; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'fail': 0.22; 'solving': 0.22; 'values': 0.23; 'exists.': 0.23; 'script.': 0.23; 'wonder': 0.24; 'example': 0.24; '(not': 0.24; 'detect': 0.25; 'received:84': 0.25; 'script': 0.26; 'windows': 0.26; 'object': 0.27; 'raise': 0.29; 'closing': 0.29; 'error': 0.29; 'problem': 0.29; 'subject:Windows': 0.29; 'user.': 0.29; 'exit': 0.29; "won't": 0.30; '---': 0.31; 'fails,': 0.31; 'operation.': 0.31; 'subject:working': 0.31; 'it.': 0.31; 'all,': 0.31; 'import': 0.32; 'to:addr:python-list': 0.32; 'source': 0.32; 'using': 0.34; 'normally': 0.35; 'that,': 0.35; 'print': 0.35; 'header:User- Agent:1': 0.35; '8bit%:89': 0.35; 'fails': 0.35; 'reply- to:addr:python.org': 0.35; 'succeeded': 0.35; 'try:': 0.35; 'none': 0.36; 'machine': 0.37; 'case': 0.37; 'issue': 0.37; 'execute': 0.38; 'facing': 0.38; 'but': 0.38; 'help': 0.39; 'end': 0.39; 'to:addr:python.org': 0.39; 'where': 0.39; 'issues': 0.39; 'except': 0.39; 'works': 0.40; 'windows.': 0.40; 'charset:windows-1252': 0.61; 'back': 0.61; 'making': 0.62; '8bit%:94': 0.65; 'destination': 0.68; 'reply-to:no real name:2**0': 0.72; 'header:Reply-To:1': 0.72; 'spaces': 0.73 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AjgIAAvyy03Unw4S/2dsb2JhbACXf411d8g0hhUElCOKQg Date: Thu, 12 May 2011 15:45:51 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 MIME-Version: 1.0 To: python-list@python.org Subject: Re: os.popen command working differently on Windows References: <21581D39FA3BDF40904D75AF9653CE26064FC6D1@blrex.prog.altair.com> In-Reply-To: <21581D39FA3BDF40904D75AF9653CE26064FC6D1@blrex.prog.altair.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org 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: 67 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305211546 news.xs4all.nl 41103 [::ffff:82.94.164.166]:49811 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5241 On 12/05/2011 15:11, Ayaskanta Swain wrote: > Hi All, > > Please help me in solving the following issue I am facing while > executing my python script. Basically I am executing the OS specific > move command to move a file/dir from one location to another. I am > executing the ‘mv’ command on linux & the ‘move’ DOS command on windows > machine from my python script. Everything works fine on linux & windows > , but the script fails to detect a particular error situation on > Windows, which is very important for my application. > > Normally the move operation fails if we try to move a directory to a > location where the same directory already exists. That means overwriting > of directories is not allowed by the OS for move operation. > > For example on Windows command prompt – *“move /Y C:\temp\my_dir > C:\stage\profiles\”* will fail with *Access is Denied* message if > C:\stage\profiles\ already contains my_dir inside it. It is an expected > OS behavior. But the problem is that, this error message is not thrown > if the command is executed from a python script. > > --- > > filemove.py script has the following code – > > ………………………………………….. > > import os > > src = sys.argv[1] > dst = sys.argv[2] > > # Using the DOS command 'move'. Adding quotes("") to avoid issues with spaces in the path. > > command = 'move' + ' ' + '/Y' + ' ' + '"' + src + '"' + ' ' + '"' + dst + '"' > > try: > os.popen(command) > except IOError: > print 'fatal: failed to execute move command' > except OSError, err: > print >> sys.stderr, "System Error while making a copy from " + src + " to " + dst > > ……………………………………………………………………………….. > > The script executes without giving any error msg and returns 0 exit > code, but in the back ground the directory is not moved to the > destination. I am providing the source & destination values as command > line arguments to the script. > > On Linux this type of use case gives me the OS error message “cannot > overwrite directory” which makes it easy for me to catch the error > message & return it the end user. But I wonder why is this not the case > on Windows. Any help to solve this will be great. > os.popen(command) will just call the command. If the command fails, its exit code will be non-zero, but that won't make Python raise an exception. os.popen(...) will return an object and you can get the exit code of the command on closing it: p = os.popen(command) exit_code = p.close() On my PC (Windows XP) I get None (not 0) if the command succeeded and 1 if it failed.