os.path.basename() - only Windows OR *nix?
Steve Holden
steve at holdenweb.com
Wed Mar 14 15:42:41 EDT 2007
Thomas Ploch wrote:
> Steve Holden schrieb:
>> Clearly if form['uploadfile'] is returning the client's path information
>> you do have to remove that somehow before further processing, which also
>> means you need to deduce what the client architecture is to correctly
>> remove path data. Of course this also leaves open the question "what
>> does a Mac client return?", and you might want to cater for that also.
>
> I tested from linux and Mac OS, and on both os.path.basename() works as
> expected (since the server it runs on is a UNIX one). That brings me to
> the question, how Do I a) get the cients architecture and b) send the
> architecture of the client through cgi.FieldStorage()?
>
Aah, right, I really meant MacOS 9 and preceding. There I understand
that the path separator was a colon, which would really hose things up
... let's assume you can ignore that.
>> I suspect you will also find that there are at least some circumstances
>> under which a Unix browser will also include path information.
>
> Which ones should that be? Since os.path.basename() _is_ doing the right
> thing on *nix style paths (Mac OS is not different there), I cant think
> of other circumstances.
>
My remark was due to a misunderstanding. The path information *is* being
sent by the browser, but os.path.basename() removes it:
>>> os.path.basename("/a/b/c.f")
'c.f'
I would suggest that all you need to do is remove any leading "X:"
(where X is any drive letter) and turn all backslashes into forward
slashes before you apply the basename function. Something like this:
filename = form['uploadfile'].filename
if filename[1] == ":":
filename = filename[2:]
filename = filename.replace("\\", "/")
filename = os.path.basename(filename)
>
>> I presume you are looking to use the same filename that the user
>> provided on the client? Does this mean that each user's files are
>> stored in different directories? Otherwise it's not always a good idea
>> to use filenames provided by the user for files on the server anyway.
>
> Yes, each User has his own directory. Files get timestamped and then put
> into the corresponding directory. It is just that having
> 'C:\a\very\long\path\file.ext' as a filename on the server is not nice.
>
I agree.
As long as there's no naming conflict between different users' files you
shouldn't have any problem.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com
See you at PyCon? http://us.pycon.org/TX2007
More information about the Python-list
mailing list