Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 18th, 2005, 05:43 AM
Thomas Steffen
Guest
 
Posts: n/a
Default file delimiter in dependency of os

Hallo,

I have pathes with different file delimiters
e.g.
/root/dir\mydir/myfile
C:\xxx/hh\gggg/myfile

How can I change the mistaken file delimiters in dependency of the
used os (win or unix) effectively?
unix: /root/dir\mydir --> /root/dir/mydir
win: C:\xxx/hh\gggg/myfile --> C:\xxx\hh\gggg\myfile

Thanks for your hints Thomas
  #2  
Old July 18th, 2005, 05:43 AM
Peter Otten
Guest
 
Posts: n/a
Default Re: file delimiter in dependency of os

Thomas Steffen wrote:
[color=blue]
> Hallo,
>
> I have pathes with different file delimiters
> e.g.
> /root/dir\mydir/myfile
> C:\xxx/hh\gggg/myfile
>
> How can I change the mistaken file delimiters in dependency of the
> used os (win or unix) effectively?
> unix: /root/dir\mydir --> /root/dir/mydir
> win: C:\xxx/hh\gggg/myfile --> C:\xxx\hh\gggg\myfile
>
> Thanks for your hints Thomas[/color]

Note that backslashes are only needed for the conventional user experience,
Windows is happy with "/", too. Have a look at os.path.normcase() which
should meet your needs if you are consistently using forward slashes in
your application. Otherwise the following tiny module might do (untested):

<plat.py>
import os
if os.name == "posix":
def normslash(path):
return path.replace("\\", "/")
elif os.name == "nt":
def normslash(path):
return path.replace("/", "\\")
else:
raise Exception("OS not supported: %s" % os.name")
</plat.py>

usage:

import plat
path = plat.normslash(path)

By the way, if you are building paths like so:

path = folder + "/" + filename

Use os.path.join() instead. It will automatically select the right separator
for you.

Peter



  #3  
Old July 18th, 2005, 05:44 AM
Peter Hansen
Guest
 
Posts: n/a
Default Re: file delimiter in dependency of os

Peter Otten wrote:[color=blue]
>
> Thomas Steffen wrote:
>[color=green]
> > Hallo,
> >
> > I have pathes with different file delimiters
> > e.g.
> > /root/dir\mydir/myfile
> > C:\xxx/hh\gggg/myfile
> >
> > How can I change the mistaken file delimiters in dependency of the
> > used os (win or unix) effectively?
> > unix: /root/dir\mydir --> /root/dir/mydir
> > win: C:\xxx/hh\gggg/myfile --> C:\xxx\hh\gggg\myfile
> >
> > Thanks for your hints Thomas[/color]
>
> Note that backslashes are only needed for the conventional user experience,
> Windows is happy with "/", too. Have a look at os.path.normcase() which[/color]

Or, to be more practical, backslashes are needed for commands that are
passed to the MS-DOS shell with os.system() and friends. Any other
way of getting the paths to the OS should work with forward slashes.

-Peter
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.