Opened 17 years ago
Closed 17 years ago
#5690 closed (worksforme)
Use os.path.dirname() not os.path.join(foo, '..')
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Core (Management commands) | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
If you use symlinks to directories using ".." will lead you
to the wrong directory.
Here is an example:
mkdir -p /tmp/dotdot/two ln -s /tmp/dotdot/two/ /tmp/symlink cd /tmp/symlink ls .. --> two Content of /tmp/symlink, not /tmp!
Attachments (1)
Change History (8)
by , 17 years ago
Attachment: | use_dirname_not_dot_dot.diff added |
---|
comment:1 by , 17 years ago
Component: | Uncategorized → django-admin.py |
---|---|
Description: | modified (diff) |
comment:2 by , 17 years ago
Using dirname()
returns the same thing as what's currently there if there is no trailing slash:
>>> directory = os.getcwd() >>> directory '/tmp/dotdot/two' >>> project_dir = os.path.normpath(os.path.join(directory, '..')) >>> project_dir '/tmp/dotdot' >>> parent_dir = os.path.basename(project_dir) >>> parent_dir 'dotdot'
and
>>> directory = os.getcwd() >>> directory '/tmp/dotdot/two' >>> project_dir = os.path.normpath(os.path.dirname(directory)) >>> project_dir '/tmp/dotdot' >>> parent_dir = os.path.basename(project_dir) >>> parent_dir 'dotdot'
However, using dirname()
returns different results if directory ends with a slash:
>>> directory = '/tmp/dotdot/two/' >>> project_dir = os.path.normpath(os.path.join(directory, '..')) >>> project_dir '/tmp/dotdot' >>> parent_dir = os.path.basename(project_dir) >>> parent_dir 'dotdot'
and
>>> directory = '/tmp/dotdot/two/' >>> project_dir = os.path.normpath(os.path.dirname(directory)) >>> project_dir '/tmp/dotdot/two' >>> parent_dir = os.path.basename(project_dir) >>> parent_dir 'two'
comment:3 by , 17 years ago
So I'm not seeing how the using of '..'
is leading to different results as you have mentioned. However, I do believe we should at least be using os.pardir
there instead of '..'
.
comment:4 by , 17 years ago
follow-up: 6 comment:5 by , 17 years ago
In django/core/management/init.py normpath() is not used. Please add it. This would
fix my problem:
comment:6 by , 17 years ago
Replying to Thomas Guettler (Home):
In django/core/management/init.py normpath() is not used. Please add it. This would
fix my problem:
We aren't going to add anything without an explanation of what the real problem is. Gary has already mentioned that he doesn't see what would be causing the problem here. Please give an explanation of what you are trying to fix so that we can ensure we make the correct fix.
comment:7 by , 17 years ago
Resolution: | → worksforme |
---|---|
Status: | new → closed |
My problem was, that the import failed, because the directory was a symlink.
I changed my setup and can't reproduce the problem. Using ".." (without
normpath) is a bad way to get the upper directory, since it can fail. See
shell example above. Nevertheless I close this ticket. Sorry for the noise.
fixed ticket formatting