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)
To use it, you just pass a path or filename into a new Path() object using forward slashes and it handles the rest: Notice two things here: You should use forward slashes with pathlib functions. The Path() object will convert forward slashes into the correct kind of slash for the current operating system.
You can use Path function from pathlib library. Use Path(r'P:\python\t\temp. txt') instead it. I specifically mentioned this case in the original question.
On Windows, paths are written using backslashes (\) as the separator between folder names. OS X and Linux, however, use the forward slash (/) as their path separator. If you want your programs to work on all operating systems, you will have to write your Python scripts to handle both cases.
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'
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With