[issue17545] os.listdir and os.path.join inconsistent on empty path
Bernard Lang added the comment: Thank you, David. BTW, I sent a message on april 20 to d...@python.org about a bug in the documentation regarding os.readlink(path) on page http://docs.python.org/2/library/os.html and proposing an alternative text. I got no reply. This was not long ago ... should I just wait ? Sorry for asking you ... I know no one else. Regards Bernard * R. David Murray rep...@bugs.python.org, le 20-04-13, a écrit: Changes by R. David Murray rdmur...@bitdance.com: -- keywords: +easy stage: - needs patch ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17545 ___ -- SVP Ne plus m'écrire à bernard.l...@inria.fr mais à l'adresse ci-dessous Please No longer write to bernard.l...@inria.fr but to the address below bernard.l...@datcha.net ,_ /\o\o/gsm +33 6 6206 1693 http://www.datcha.net/ ^ tel +33 1 3056 1693 Je n'exprime que mon opinion - I express only my opinion CAGED BEHIND WINDOWS or FREE WITH LINUX -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17545 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue17545] os.listdir and os.path.join inconsistent on empty path
Bernard Lang added the comment: Reply to R.David.Murray See also issue 6095. You are right. I goofed, this is the issue I meant to point to. $ ls '' ls: cannot access : No such file or directory So, the behavior is consistent with the shell. This is a fair remark. But still, giving a meaning to something that has none in the shell would not be an inconsistency either. There are lots of other differences in the Unix shell design, and strings are often used as a syntactic device. You do not have to quote file names or paths, unless they raise syntactic problems. Now if you look at path manipulation commands in the shell, you have : $ basename aaa aaa $ dirname aaa . This is a fair choice for the shell since an empty path would print as nothing. Furthermore, string manipulation is not as convenient with the shell as it is with Python. So the shell is altogether ignoring the empty path, and string manipulations (possibly using the empty string) to build a path representation are not part of the path system. Python has already made a different choice. In [4]: os.path.basename('aaa') Out[4]: 'aaa' In [5]: os.path.dirname('aaa') Out[5]: '' These are the two results of os.path.split('aaa'), which is somewhat the inverse of os.path.join(...) which I initially considered. So os.path.dirname in Python does return the empty string '' where the shell dirname returns a dot. This could also be seen as an inconsistency between Unix shell and Python. However, the Unix shell is internally consistent, while taking into account its own specific constraints. It seems that it is more important for Python to similarly have its own internal consistency, especially when considering that Python is already departing from Unix shell in some minor ways, which are related to the internal consistency issue that was raised. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17545 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue17545] os.listdir and os.path.join inconsistent on empty path
New submission from Bernard Lang: The empty path '' is considered as an acceptable path in os.path.join, and works as a neutral prefix: print os.path.join('','aaa') === aaa which seems rather natural. But it raises an exception when used as a parameter to os.listdir. Logically, it should then list the current directory. (the alternative would be to raise an exception when os.path.join gets an empty argument). The inconsistency became clear to me when I had to write the following function : def listdirs(path,flist): # Appends to flist all paths to files that # start with path. Argument path can be empty string ''. if path=='' : localist=os.listdir('.') else : localist=os.listdir(path) for f in localist : p=os.path.join(path,f) flist.append(p) if os.path.isdir(p) : listdirs(p,flist) return flist The conditionnal is needed only to avoid the exception, while the code is unique afterwards. This is related to Issue818059, but I did not see how that issue was resolved. Furthermore, the case of the empty path as argument to os.listdir should be documented in http://docs.python.org/2/library/os.html -- components: Library (Lib) messages: 185207 nosy: babou priority: normal severity: normal status: open title: os.listdir and os.path.join inconsistent on empty path type: enhancement versions: Python 2.7 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17545 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com