This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: os.environ.get treats Environt variant with double quotation marks wrong
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: r.david.murray, 进陆
Priority: normal Keywords:

Created on 2015-06-17 09:51 by 进陆, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg245433 - (view) Author: 进陆 (进陆) Date: 2015-06-17 09:51
On windows, if a Directory/Filename has SPACE, double quotation mark should be used. For example, "R:\just a test\hello.bat" has only one line "@echo hello world", so in the dos prompt
[quote]
R:\just a test>hello.bat
hello world

R:\just a test>cd ..

R:\>set dir1="R:\just a test"

R:\>set dir2=R:\just a test

R:\>%dir1%\hello.bat
hello world

R:\>%dir2%\hello.bat
'R:\just' is not recognized as an internal or external command 
[/quote]

Then, in python (it seems to be a common problem in all(?) python version)
[quote]
dir1= os.environ.get('dir1')
print (dir1)
print (os.path.isdir(dir1))
print (os.path.isdir(dir1.replace('"', '')))

print ('*'*10)

dir2= os.environ.get('dir2')
print (dir2)
print (os.path.isdir(dir2))
print (os.path.isdir(dir2.replace('"', '')))
[/quote]
displays
[quote]
"R:\just a test"
False                  <--obviously, the double quotation marks is treated as the part of the directory name by python, so the answer is wrong
True
**********
R:\just a test
True
True
[/quote]

the patched method is simple
[quote]
if os.name=='nt':
    res=res.replace('"', '')
[/quote]
but the not simple thing is that os.py for python 3.4.3 changes a lot than os.py for python 2.7, I get lost while reading os.py for python 3.4.3 by no IDE. So I just report this issue, sorry.
msg245434 - (view) Author: 进陆 (进陆) Date: 2015-06-17 09:59
the patched method should be
[quote]
if os.name=='nt' and res:
    res=res.replace('"', '')
[/quote]
msg245438 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-06-17 14:37
This is working in pretty much the only way it can work.  Python is correctly retrieving the string from the environment (it includes the quotation marks).  It is correctly passing that string to os.path: os.path takes the exact string that represents the filename.  In windows
it would be equally valid to do this:

    set dir3="just "" a"" test"
    %dir3%\hello.bat

Your "simple fix" would not address that case (and would break existing programs that rely on the current behavior).  There is no way to handle all the possible variations here.  The only thing that can be done is what python does do: return exactly the string stored in the environment variable, and expect exactly the filename as input to functions that expect filenames.

In many contexts in Windows it works perfectly fine to have the filename without quotes around it stored in the environment variable and to use that:

  dir %dir1%

works fine, for example.  The Windows rules for quoting are non-obvious and not completely consistent, I'm afraid, and there's nothing python can do to paper over that fact.
History
Date User Action Args
2022-04-11 14:58:18adminsetgithub: 68649
2015-06-17 14:37:17r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg245438

resolution: not a bug
stage: resolved
2015-06-17 09:59:07进陆setmessages: + msg245434
2015-06-17 09:51:02进陆create