Python - Make a script to manage Windows file paths, but works on Linux

I have this script that processes strings containing Windows file paths. However, the script works on Linux. Is there a way to change the os library to handle the Windows file path while running on Linux?

I thought something like:

import os
os.pathsep = '\\'

(which does not work since os.pathsep for any 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)
+5
source share
3 answers

Take a look at ntpath

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'
+7
source

Try to use os.sep = '\\'. os.pathsep is the delimiter used to split the search path (PATH environment variable) into os.

. os

+3

os.pathsep - , PATH. os.sep.

Although I would generally advise you not to modify the data in such a module, this can satisfy your needs. Alternatively, you can implement basename yourself, something likepackage_path.split('\\')[-1]

+1
source

All Articles