os.listdir default to '.'

Hello all, I frequently use Python from the interactive interpreter as a shell. It is a frequent, but mild, irritant that I have to type os.listdir('.') instead of just os.listdir(). How about making '.' a default argument for os.listdir ? All the best, Michael -- http://www.voidspace.org.uk/ May you do good and not evil May you find forgiveness for yourself and forgive others May you share freely, never taking more than you give. -- the sqlite blessing http://www.sqlite.org/different.html

In Python 2.5 you can at least say os.listdir('') which had the same effect. In Python 2.7 you can't. Strictly speaking I think the default value should be os.path.curdir, not specifically '.' +1 from me +1 on allowing the empty string argument again (may avoid breaking some old code) Rob Cliffe On 12/01/2012 15:02, Michael Foord wrote:

On 12Jan2012 15:14, Rob Cliffe <rob.cliffe@btinternet.com> wrote: | In Python 2.5 you can at least say os.listdir('') which had the same effect. | In Python 2.7 you can't. | | Strictly speaking I think the default value should be | os.path.curdir, not specifically '.' | +1 from me I think it should be '.'. It is possible to arrange directory permissions so that you can't open curdir because it traverses a directory to which one lacks permissions, but you can still open '.'. Correspondingly, there can be times you can opendir('.') but not compute curdir (for access reasons); an unpleasant situation but possible (maybe not on platforms where the OS hands you curdir like Linux; historically UNIX platforms compute it by consulting the mount table and walking some directories. | +1 on allowing the empty string argument again (may avoid breaking | some old code) I used to be a fan of the '' UNIX string referring to the current directory (on the tenuous but cool logic that '' lets the kernel hand you the current directory directly, while '.' implies _opening_ the current directory and finding its '.' entry, and opening that:-) but not any more - it makes more trouble than its worth (and I think it went away in POSIX). '.' has better lexical properties than '' - it makes dirname and basename and similar operations easier to code and think about because it is not an empty string. opendir() should be as thin a glue around the OS's opendir as possible. On that basis I'm good with a default argument value but bad on explicitly supporting '' as meaning the current directory when the OS may not; it would at least mean wiring special knowledge into os.opendir for that string. I guess I'm: +1 for '.' being opendir's default. -0.5 for '' being opendir's default. -1 for os.curdir() being opendir's default. Cheers, -- Cameron Simpson <cs@zip.com.au> DoD#743 http://www.cskk.ezoshosting.com/cs/ I'M NOT afraid of insects talking over the world, and you know why? It would take about a billion ant just to aim a gun at me, let alone fire it. And know what I'm doing while they're aiming ti at me? I just sort of slip off to the side, and then suddenly run up and kick the gun out of their hands. - Jack Handey

Cameron Simpson schrieb am Fr, 13. Jan 2012, um 08:32:23 +1100:
There's a misunderstanding here. You are confusing 'os.getcwd()' and 'os.curdir'. The default value for the parameter of 'os.listdir()' should probably be 'os.curdir', but since it is defined in the different platform-specific OS modules like posixmodule.c, it probably is 'os.curdir'. Moreover, I'm not aware of a platform where os.curdir != '.' -- Sven

On 12Jan2012 22:11, Sven Marnach <sven@marnach.net> wrote: | Cameron Simpson schrieb am Fr, 13. Jan 2012, um 08:32:23 +1100: | > On 12Jan2012 15:14, Rob Cliffe <rob.cliffe@btinternet.com> wrote: | > | Strictly speaking I think the default value should be | > | os.path.curdir, not specifically '.' | [...] | > -1 for os.curdir() being opendir's default. | | There's a misunderstanding here. You are confusing 'os.getcwd()' and | 'os.curdir'. Yes. Please ignore the rest of that post, based as it is on a brain fart on my part. Cheers, -- Cameron Simpson <cs@zip.com.au> DoD#743 http://www.cskk.ezoshosting.com/cs/ It must be public fact, because I'm not the only one who knows about it. - Stefan A. Werner <iczer-1!saw@uunet.uu.net>

Cameron Simpson <cs@zip.com.au> writes:
What distinction are you drawing? >>> os.path.curdir '.' Do you get a different result? -- \ “Working out the social politics of who you can trust and why | `\ is, quite literally, what a very large part of our brain has | _o__) evolved to do.” —Douglas Adams | Ben Finney

On 13Jan2012 09:26, Ben Finney <ben+python@benfinney.id.au> wrote: | Cameron Simpson <cs@zip.com.au> writes: | > I think it should be '.' [instead of ‘os.path.curdir’]. | > | > It is possible to arrange directory permissions so that you can't open | > curdir because it traverses a directory to which one lacks permissions, | > but you can still open '.'. | | What distinction are you drawing? | | >>> os.path.curdir | '.' A totally bogus distinction. I had os.getcwd() in my head instead. Please ignore my post:-( Cheers, -- Cameron Simpson <cs@zip.com.au> DoD#743 http://www.cskk.ezoshosting.com/cs/ This signature was originally recorded on analog equipment. While we have attempted to preserve the meaning of the original, the high resolution of this media may reveal limitations of the source. - paul-michael agapow <agapow@latcs1.oz.au>

On Thu, Jan 12, 2012 at 03:02:52PM +0000, Michael Foord wrote:
Not exactly what you've asked but let me show you my config for interactive sessions: http://phdru.name/Software/dotfiles/init.py.html You can type
cd('/etc') ls
and got back the list. (-: Oleg. -- Oleg Broytman http://phdru.name/ phd@phdru.name Programmers don't die, they just GOSUB without RETURN.

Michael Foord <fuzzyman@...> writes:
Hello all,I frequently use Python from the interactive interpreter as a shell.
It is a frequent, but mild, irritant that I have to type os.listdir('.') instead of just os.listdir().How about making '.' a default argument for os.listdir ?All the best,Michael-- $ ./python Python 3.3.0a0 (default:2db3ca05fbb7+, Jan 11 2012, 20:46:04) [GCC 4.5.3] on linux Type "help", "copyright", "credits" or "license" for more information.

IIRC for Python 2 under Windows os.listdir(".") does something different than os.listdir(u"."). Because I think there are two filesystem APIs under Windows, one using char and the other wchar_t. (I don't use Windows so I could be mistaken.) But anyway, under any OS Python 2 returns something else whether you pass a unicode or str object to listdir (a list of unicode or string objects). Under Python 3 there is no unicode because str is what under Python 2 was unicode. So under Python 2 os.listdir() would be not good. What should it return? strs or unicodes? On 01/12/2012 04:02 PM, Michael Foord wrote:

On 2012-01-12, at 19:46 , Mathias Panzenböck wrote:
But anyway, under any OS Python 2 returns something else whether you pass a unicode or str object to listdir (a list of unicode or string objects). Under Python 3 there is no unicode because str is what under Python 2 was unicode.
So under Python 2 os.listdir() would be not good. What should it return? strs or unicodes?
I understand Python 2 is in maintenance-only status anyway, so there's no way python-ideas will apply to it. And since Python 3 seems to already have os.listdir default to '.'...

On 1/12/2012 10:02 AM, Michael Foord wrote:
How about making '.' a default argument for os.listdir ?
From 3.2 doc: "os.listdir(path='.')" In general, to find out what is already in future versions, the development docs (now 3.3a0, but later 3.4a0, etc) are at http://docs.python.org/dev/ Enhancement issues on the tracker show existing proposals. -- Terry Jan Reedy

In Python 2.5 you can at least say os.listdir('') which had the same effect. In Python 2.7 you can't. Strictly speaking I think the default value should be os.path.curdir, not specifically '.' +1 from me +1 on allowing the empty string argument again (may avoid breaking some old code) Rob Cliffe On 12/01/2012 15:02, Michael Foord wrote:

On 12Jan2012 15:14, Rob Cliffe <rob.cliffe@btinternet.com> wrote: | In Python 2.5 you can at least say os.listdir('') which had the same effect. | In Python 2.7 you can't. | | Strictly speaking I think the default value should be | os.path.curdir, not specifically '.' | +1 from me I think it should be '.'. It is possible to arrange directory permissions so that you can't open curdir because it traverses a directory to which one lacks permissions, but you can still open '.'. Correspondingly, there can be times you can opendir('.') but not compute curdir (for access reasons); an unpleasant situation but possible (maybe not on platforms where the OS hands you curdir like Linux; historically UNIX platforms compute it by consulting the mount table and walking some directories. | +1 on allowing the empty string argument again (may avoid breaking | some old code) I used to be a fan of the '' UNIX string referring to the current directory (on the tenuous but cool logic that '' lets the kernel hand you the current directory directly, while '.' implies _opening_ the current directory and finding its '.' entry, and opening that:-) but not any more - it makes more trouble than its worth (and I think it went away in POSIX). '.' has better lexical properties than '' - it makes dirname and basename and similar operations easier to code and think about because it is not an empty string. opendir() should be as thin a glue around the OS's opendir as possible. On that basis I'm good with a default argument value but bad on explicitly supporting '' as meaning the current directory when the OS may not; it would at least mean wiring special knowledge into os.opendir for that string. I guess I'm: +1 for '.' being opendir's default. -0.5 for '' being opendir's default. -1 for os.curdir() being opendir's default. Cheers, -- Cameron Simpson <cs@zip.com.au> DoD#743 http://www.cskk.ezoshosting.com/cs/ I'M NOT afraid of insects talking over the world, and you know why? It would take about a billion ant just to aim a gun at me, let alone fire it. And know what I'm doing while they're aiming ti at me? I just sort of slip off to the side, and then suddenly run up and kick the gun out of their hands. - Jack Handey

Cameron Simpson schrieb am Fr, 13. Jan 2012, um 08:32:23 +1100:
There's a misunderstanding here. You are confusing 'os.getcwd()' and 'os.curdir'. The default value for the parameter of 'os.listdir()' should probably be 'os.curdir', but since it is defined in the different platform-specific OS modules like posixmodule.c, it probably is 'os.curdir'. Moreover, I'm not aware of a platform where os.curdir != '.' -- Sven

On 12Jan2012 22:11, Sven Marnach <sven@marnach.net> wrote: | Cameron Simpson schrieb am Fr, 13. Jan 2012, um 08:32:23 +1100: | > On 12Jan2012 15:14, Rob Cliffe <rob.cliffe@btinternet.com> wrote: | > | Strictly speaking I think the default value should be | > | os.path.curdir, not specifically '.' | [...] | > -1 for os.curdir() being opendir's default. | | There's a misunderstanding here. You are confusing 'os.getcwd()' and | 'os.curdir'. Yes. Please ignore the rest of that post, based as it is on a brain fart on my part. Cheers, -- Cameron Simpson <cs@zip.com.au> DoD#743 http://www.cskk.ezoshosting.com/cs/ It must be public fact, because I'm not the only one who knows about it. - Stefan A. Werner <iczer-1!saw@uunet.uu.net>

Cameron Simpson <cs@zip.com.au> writes:
What distinction are you drawing? >>> os.path.curdir '.' Do you get a different result? -- \ “Working out the social politics of who you can trust and why | `\ is, quite literally, what a very large part of our brain has | _o__) evolved to do.” —Douglas Adams | Ben Finney

On 13Jan2012 09:26, Ben Finney <ben+python@benfinney.id.au> wrote: | Cameron Simpson <cs@zip.com.au> writes: | > I think it should be '.' [instead of ‘os.path.curdir’]. | > | > It is possible to arrange directory permissions so that you can't open | > curdir because it traverses a directory to which one lacks permissions, | > but you can still open '.'. | | What distinction are you drawing? | | >>> os.path.curdir | '.' A totally bogus distinction. I had os.getcwd() in my head instead. Please ignore my post:-( Cheers, -- Cameron Simpson <cs@zip.com.au> DoD#743 http://www.cskk.ezoshosting.com/cs/ This signature was originally recorded on analog equipment. While we have attempted to preserve the meaning of the original, the high resolution of this media may reveal limitations of the source. - paul-michael agapow <agapow@latcs1.oz.au>

On Thu, Jan 12, 2012 at 03:02:52PM +0000, Michael Foord wrote:
Not exactly what you've asked but let me show you my config for interactive sessions: http://phdru.name/Software/dotfiles/init.py.html You can type
cd('/etc') ls
and got back the list. (-: Oleg. -- Oleg Broytman http://phdru.name/ phd@phdru.name Programmers don't die, they just GOSUB without RETURN.

Michael Foord <fuzzyman@...> writes:
Hello all,I frequently use Python from the interactive interpreter as a shell.
It is a frequent, but mild, irritant that I have to type os.listdir('.') instead of just os.listdir().How about making '.' a default argument for os.listdir ?All the best,Michael-- $ ./python Python 3.3.0a0 (default:2db3ca05fbb7+, Jan 11 2012, 20:46:04) [GCC 4.5.3] on linux Type "help", "copyright", "credits" or "license" for more information.

IIRC for Python 2 under Windows os.listdir(".") does something different than os.listdir(u"."). Because I think there are two filesystem APIs under Windows, one using char and the other wchar_t. (I don't use Windows so I could be mistaken.) But anyway, under any OS Python 2 returns something else whether you pass a unicode or str object to listdir (a list of unicode or string objects). Under Python 3 there is no unicode because str is what under Python 2 was unicode. So under Python 2 os.listdir() would be not good. What should it return? strs or unicodes? On 01/12/2012 04:02 PM, Michael Foord wrote:

On 2012-01-12, at 19:46 , Mathias Panzenböck wrote:
But anyway, under any OS Python 2 returns something else whether you pass a unicode or str object to listdir (a list of unicode or string objects). Under Python 3 there is no unicode because str is what under Python 2 was unicode.
So under Python 2 os.listdir() would be not good. What should it return? strs or unicodes?
I understand Python 2 is in maintenance-only status anyway, so there's no way python-ideas will apply to it. And since Python 3 seems to already have os.listdir default to '.'...

On 1/12/2012 10:02 AM, Michael Foord wrote:
How about making '.' a default argument for os.listdir ?
From 3.2 doc: "os.listdir(path='.')" In general, to find out what is already in future versions, the development docs (now 3.3a0, but later 3.4a0, etc) are at http://docs.python.org/dev/ Enhancement issues on the tracker show existing proposals. -- Terry Jan Reedy
participants (12)
-
Ben Finney
-
Benjamin Peterson
-
Cameron Simpson
-
Guido van Rossum
-
Masklinn
-
Massimo Di Pierro
-
Mathias Panzenböck
-
Michael Foord
-
Oleg Broytman
-
Rob Cliffe
-
Sven Marnach
-
Terry Reedy