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


Groups > comp.lang.python > #59116

Re: Creating a function for a directory

References <521ed3d4-e2f7-4196-947d-61f94573a8ce@googlegroups.com> <adf24d2c-1974-4c09-9ff2-bc82b31a09f4@googlegroups.com>
Date 2013-11-12 10:11 +1100
Subject Re: Creating a function for a directory
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2408.1384211515.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Nov 12, 2013 at 9:51 AM, Rick Johnson
<rantingrickjohnson@gmail.com> wrote:
> On Monday, November 11, 2013 4:26:46 PM UTC-6, Matt wrote:
>
>> So I want to take the file, "desktop/test.txt" and write
>> it to "desktop/newfolder/test.txt". I tried the below
>> script, and it gave me: "IOError: [Errno 2] No such file
>> or directory: 'desktop/%s.txt'". Any suggestions would be
>> great.
>>
>> def firstdev(file):
>>       in_file = open("desktop/%s.txt") % file
>>       indata = in_file.read()
>>       out_file = open("desktop/newfolder/%s.txt", 'w') % file
>>       out_file.write(indata)
>>       out_file.close()
>>       in_file.close()
>> firstdev("test")
>
> 1. i believe win32 file paths require a qualifying volume
> letter.

They do not; omitting the drive letter makes the path relative to the
current drive (and since it doesn't start with a directory specifier,
the current directory).

> 2. Never, ever, *EVER* write data to disc before confirming
> the paths your passing are pointing to the location you
> intended to write the data. Use os.path.exists(path) to test
> your paths BEFORE trying to write data.

Why? Why, oh why? If there's a problem, it'll be signalled with an
exception. Testing that the path exists opens you up to race problems,
so you won't see anything now, but some day your code will be in a
concurrent situation and you'll get unexpected exceptions. Why not
just expect the exception?

> 3. Be sure your variables names are both "self documenting"
> and "non clobbering". psst: "file" is a builtin! Using
> "filename" would be a far wiser choice for a variable
> containing a filename. When i see "file", i think of a "file
> object"

This one's arguable. How often do you use the 'file' builtin? I've
worked with files in Python innumerable times, and I don't remember
the last time I used 'file'. Yes, avoid shadowing important builtins
like 'list' and 'int', but 'id' and 'file' aren't that big a deal.

> 4. When dealing with files you must be sure that exceptions
> are handled cleanly. You don't want open file objects
> floating aimlessly around in memory because your naive code
> blew chunks.

They won't float around forever. The garbage collector will get to
them. If you're advocating use of 'with', that's only going to be an
issue if the code's called in a loop AND if it throws before the
close() calls come through.

> 5. Remember, you cannot write a file into a directory that
> does not exist.

So? Exception thrown, traceback printed to console, process terminated
cleanly. I'm not seeing a problem here.

> 6 For OS compatibility always use os.path.join() to join
> path parts into a whole. This method will insert the proper
> separator for you depending on the OS.

Technically true. However, most modern OSes will accept a slash.
There'll be a few situations where that's not true, but the OP an
happily just use slashes for simplicity. Remember the zen:
Practicality beats purity.

ChrisA

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


Thread

Creating a function for a directory Matt <mattgraves7@gmail.com> - 2013-11-11 14:26 -0800
  Re: Creating a function for a directory Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-11 17:36 -0500
  Re: Creating a function for a directory Chris Angelico <rosuav@gmail.com> - 2013-11-12 09:38 +1100
  Re: Creating a function for a directory bob gailer <bgailer@gmail.com> - 2013-11-11 17:42 -0500
  Re: Creating a function for a directory Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-11 17:44 -0500
  Re: Creating a function for a directory Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-11 22:45 +0000
  Re: Creating a function for a directory Rick Johnson <rantingrickjohnson@gmail.com> - 2013-11-11 14:51 -0800
    Re: Creating a function for a directory Chris Angelico <rosuav@gmail.com> - 2013-11-12 10:11 +1100
      Re: Creating a function for a directory Rick Johnson <rantingrickjohnson@gmail.com> - 2013-11-11 21:42 -0800
        Re: Creating a function for a directory Chris Angelico <rosuav@gmail.com> - 2013-11-12 17:02 +1100
    Re: Creating a function for a directory Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-12 00:33 +0000
      Re: Creating a function for a directory Neil Cerutti <neilc@norwich.edu> - 2013-11-12 13:42 +0000
  Re: Creating a function for a directory Matt <mattgraves7@gmail.com> - 2013-11-11 15:05 -0800
  Re: Creating a function for a directory Chris Angelico <rosuav@gmail.com> - 2013-11-12 09:51 +1100
  Re: Creating a function for a directory unknown <unknown@unknown.com> - 2013-11-12 10:07 +0000
    Re: Creating a function for a directory Peter Otten <__peter__@web.de> - 2013-11-12 11:24 +0100
      Re: Creating a function for a directory unknown <unknown@unknown.com> - 2013-11-12 11:26 +0000

csiph-web