classification
Title: Allow shell like paths in
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7, Python 3.6, Python 3.5, Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: gavin, massimosala, serhiy.storchaka, xtreak
Priority: normal Keywords:

Created on 2020-04-13 10:33 by gavin, last changed 2020-04-16 16:38 by serhiy.storchaka. This issue is now closed.

Messages (7)
msg366297 - (view) Author: Gavin D'souza (gavin) Date: 2020-04-13 10:33
Related library: zipfile

Since zipfile allows relative dotted paths too, should shell-like paths, specifically with ~ (tilde) be allowed too?

This feels like unexpected behaviour and confusing for users as method "is_zipfile" returns False in case path doesn't exist as well. So, zipfile.is_zipfile("~/incorrect/path/to/zip") as well as zipfile.is_zipfile("~/path/to/zip") will return False
msg366302 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-04-13 10:50
You can use os.path.expanduser to expand tilde. Other os functions don't do it implicitly.

>>> import os
>>> os.path.exists("~/stuff")
False
>>> os.path.exists(os.path.expanduser("~/stuff"))
True
msg366306 - (view) Author: Gavin D'souza (gavin) Date: 2020-04-13 11:57
Thank you @xtreak, I'm aware of "os.path.expanduser" and have used it extensively; I created this issue if we could do something about handling this internally in zipfile too.
msg366310 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-04-13 12:43
How would you work with a file in the "~" directory?

Python is a programming language. It provides you builtin blocks which you can combine to get the desired behavior. If you what "~" at the start of the path be expanded to your home directory, you call os.path.expanduser() explicitly. If you want it mean the literal "~" directory, you do not call os.path.expanduser(). Calling it implicitly would break existing code, require you to use ugly workarounds if you don't want to expand "~", and introduce possible security issues.
msg366377 - (view) Author: Gavin D'souza (gavin) Date: 2020-04-14 12:20
@serhiy.storchaka That makes perfect sense. Could we do something to add a parameter perhaps, to evaluate literal paths to not bread existing code? although this isn't "needed" but it'd be neat to handle this internally
msg366599 - (view) Author: Massimo Sala (massimosala) * Date: 2020-04-16 14:21
Gavin, zipfile works on all the operating systems where python runs.
Your request is OS dependent... BSD? linux?

The tilde isn't into the ZIP file specifications.
I have to agree with Serhiy: the correct solution is
    os.path.expanduser("~/stuff")
msg366607 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-04-16 16:38
If add such option to zipfile.is_zipfile(), why not add it to other functions? There are many tens or hundreds of functions and methods in the stdlib which accept a file path. Adding such option to all of them is not practical. And zipfile.is_zipfile() does not look special.

Also, there are other options which you may want to add to zipfile.is_zipfile(). What about expandvars()? Or support URIs with the file:/// scheme? Or maybe someone want to replace ~ with the project directory instead of the home directory.

It is better to provide functions for every tiny feature and combine them as you want than add an infinite number of options to all functions.
History
Date User Action Args
2020-04-16 16:38:47serhiy.storchakasetstatus: open -> closed
resolution: rejected
messages: + msg366607

stage: resolved
2020-04-16 14:21:10massimosalasetnosy: + massimosala
messages: + msg366599
2020-04-14 12:20:16gavinsetmessages: + msg366377
2020-04-13 12:43:07serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg366310
2020-04-13 11:57:21gavinsetmessages: + msg366306
2020-04-13 10:50:48xtreaksetnosy: + xtreak
messages: + msg366302
2020-04-13 10:33:50gavincreate