Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #88496
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <cameron@cskk.homeip.net> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'assignment': 0.07; 'clause': 0.09; 'except:': 0.09; 'friday,': 0.09; 'function,': 0.09; 'try:': 0.09; 'wrong,': 0.09; 'subject:How': 0.10; 'python': 0.11; 'windows': 0.15; '3.3,': 0.16; '>>on': 0.16; '>on': 0.16; 'checking,': 0.16; 'complain,': 0.16; 'exists,': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'mkdir': 0.16; 'oserror': 0.16; 'simpson': 0.16; 'subject:Problem': 0.16; 'tends': 0.16; 'wrote:': 0.18; 'slightly': 0.19; 'header:User-Agent:1': 0.23; 'earlier': 0.24; 'cheers,': 0.24; 'this:': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'unix': 0.29; 'words': 0.29; 'went': 0.31; '>>>>': 0.31; 'file': 0.32; 'probably': 0.32; 'checking': 0.33; 'level.': 0.33; 'not.': 0.33; 'could': 0.34; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'charset:us- ascii': 0.36; 'subject:?': 0.36; 'should': 0.36; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'little': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'ensure': 0.60; 'catch': 0.60; 'dave': 0.60; 'content-disposition:inline': 0.62; 'act': 0.63; 'brain': 0.68; 'subject:this': 0.83; '2015': 0.84; 'received:192.168.15': 0.84; 'remark': 0.84; 'angel': 0.91; 'subject:Best': 0.91 |
| Date | Sat, 4 Apr 2015 12:31:34 +1100 |
| From | Cameron Simpson <cs@zip.com.au> |
| To | python-list@python.org |
| Subject | Re: Strategy/ Advice for How to Best Attack this Problem? |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii; format=flowed |
| Content-Disposition | inline |
| In-Reply-To | <551EF637.80801@davea.name> |
| User-Agent | Mutt/1.5.23 (2014-03-12) |
| References | <551EF637.80801@davea.name> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://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 | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.40.1428111110.12925.python-list@python.org> (permalink) |
| Lines | 38 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1428111110 news.xs4all.nl 2831 [2001:888:2000:d::a6]:49894 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:88496 |
Show key headers only | View raw
On 03Apr2015 16:21, Dave Angel <davea@davea.name> wrote:
>On 04/03/2015 08:50 AM, Saran A wrote:
>>On Friday, April 3, 2015 at 8:05:14 AM UTC-4, Dave Angel wrote:
>>>On 04/02/2015 07:43 PM, Saran A wrote:
>>>> os.mkdir('Success')
>
>>As you correctly stated:
>>>What do you do the second time through this function, when that
>>>directory is already existing?
>>>> copy_and_move_file( 'Failure')
[...]
>>How would I ensure that this s directory is made only once and every file that is passeed goes only to 'success' or 'failure'?
>
>Well, you could use an if clause checking with os.exist(). If the
>directory already exists, don't call the mkdir function. That may not
>be perfect, but it should suffice for an assignment at your level.
As a general remark that's slightly racy: checking, then doing. On all UNIX
platforms, and probably Windows and others, mkdir is atomic: it works or it
does not. So the reliable way tends to look like this:
try:
os.mkdir("blah")
except FileExistsError:
... the directory exists, act accordingly ...
except:
... something else went wrong, complain, abort, whatever
else:
... directory successfully made ...
FileExistsError only came in in Python 3.3, for earlier Pythons catch OSError
and check the exception's .errno against errno.EEXIST.
Cheers,
Cameron Simpson <cs@zip.com.au>
I am a Bear of Very Little Brain and long words Bother Me.
- Winnie-the-Pooh
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Strategy/ Advice for How to Best Attack this Problem? Cameron Simpson <cs@zip.com.au> - 2015-04-04 12:31 +1100
csiph-web