views:

46

answers:

3

I have this script which processes lines containing windows file paths. However the script is running on Linux. Is there a way to change the os library to do Windows file path handling while running on linux?

I was thinking something like:

import os
os.pathsep = '\\'

(which doesn't work since os.pathsep is ; for some reason)

My script:

for line in INPUT.splitlines():
    package_path,step_name = line.strip().split('>')
    file_name = os.path.basename(package_path)
    name = os.path.splitext(file_name)[0]
    print template % (name,file_name,package_path)
+1  A: 

os.pathsep is the separator that is used for the PATH environment variable. You're looking for os.sep.

While I would generally advise against changing data in a module like that, it may suit your needs. Alternatively, you could implement basename yourself, something like package_path.split('\\')[-1]

dash-tom-bang
+2  A: 

Try using os.sep = '\\'. os.pathsep is the separator used to separate the search path (PATH environment variable) on the os.

see os module description

Rod
+4  A: 

Look at the ntpath module

On Linux, I did:

>> import ntpath      
>> ntpath.split("c:\windows\i\love\you.txt")
('c:\\windows\\i\\love', 'you.txt')
>> ntpath.splitext("c:\windows\i\love\you.txt")
('c:\\windows\\i\\love\\you', '.txt')
>> ntpath.basename("c:\windows\i\love\you.txt")
'you.txt'
Mark