Splitting a simple specific path on windows and linux - python

Problem:
I'm having different behavior for Windows and Linux in the following case.
import os
path = '..\\file.hdf'
norm_path = os.path.normpath(path)
splitted_path = os.path.split(norm_path)
print(splitted_path)
Behavior
On Windows I get ('', 'file.hdf')
On Linux I get ('', '..\\file.hdf')
Question
Is there a better/specific way to use the os.path for this?
Workaround
Ok, it's easily fixed with norm_path.split('\\'), but that's not dynamic at all.

On Linux, paths are separated with a forward slash. If you want a platform-independent approach, I suggest using os.sep instead of backslash:
import os
path = '..' + os.sep + 'file.hdf'
norm_path = os.path.normpath(path)
split_path = os.path.split(norm_path)
print(split_path)

In Linux \ is NOT a path separator. Therefore, your ..\\file.hdf means "a file named file.hdf in the parent directory of the current directory" on Windows, but simply "a file named ..\file.hdf in current directory" on Linux. I suggest using pathlib module instead of os.path:
import pathlib
norm_path = pathlib.PureWindowsPath('..\\file.hdf')
split_path = list(norm_path.parts)
# ['..', 'file.hdf'] both on Linux and Windows

replace \\ with /
Windows can handle / as path separator.
Linux can't handle \\
So use / for any code you want to be able to be running on Linux and Windows
Or do it the really clean way by using os.sep as #snibbets suggests.
In that case I'd use os.sep.join('..', 'file.hdf')

Related

Identify String as Windows or UNIX like path in Python

How do I best identify if a String is a Windows path or a UNIX style path?
Example Strings:
some_path = '/Volumes/network-drive/file.txt'
or
some_path = 'Z:\\network-drive\\file.txt'
One way is to check which slashes the String contains:
if '/' in some_path:
# do something with UNIX Style Path
elif '\\' in some_path:
# do something else with Windows Path
Is there a better way to do this? I couldn't find suited methods in os.path or pathlib. BTW, assume that the path string will come from another system so it doesn't help to check on which OS my code runs on.

How do you define a local path in python

Good morning, I can indicate how to enter a path of internal hard disk in python, currently use the statement:
file = GETfile() or 'http://**********'
I would like to put a path to a local file, but it does not work, where am I wrong?
file = GETfile() or 'D:\xxx\xxxx\playlist\playlist.m3u'
\ is a escape character. You have three options.
1) use /. This, as a bonus works for linux as well:
'D:/xxx/xxxx/playlist/playlist.m3u'
2) escape the backslash
'D:\\xxx\\xxxx\\playlist\\playlist.m3u'
3) use raw strings:
r'D:\xxx\xxxx\playlist\playlist.m3u'
A correct answer is already given, but some additional information when working with local drive paths on Windows operating system.
Personally I would go with the r'D:\dir\subdir\filename.ext' format, however the other two methods already mentioned are valid as well.
Furthermore, file operations on Windows are limited by Explorer to a 256 character limit. Longer path names will usually result in an OS error.
However there is a workaround, by pre fixing "\\?\" to a long path.
Example of a path which does not work:
D:\reallyreallyreallyreallyreallylonglonglonglongdir\reallyreallyreallyreallyreallylonglonglonglongdir\reallyreallyreallyreallyreallylonglonglonglongdir\reallyreallyreallyreallyreallylonglonglonglongdir\reallyreallyreallyreallyreallylonglonglonglongdir\reallyreallyreallyreallyreallylonglonglonglongdir\reallyreallyreallyreallyreallylonglonglonglongdir\reallyreallyreallyreallyreallylonglonglonglongdir\filename.ext
Same file path which does work:
\\?\D:\reallyreallyreallyreallyreallylonglonglonglongdir\reallyreallyreallyreallyreallylonglonglonglongdir\reallyreallyreallyreallyreallylonglonglonglongdir\reallyreallyreallyreallyreallylonglonglonglongdir\reallyreallyreallyreallyreallylonglonglonglongdir\reallyreallyreallyreallyreallylonglonglonglongdir\reallyreallyreallyreallyreallylonglonglonglongdir\reallyreallyreallyreallyreallylonglonglonglongdir\filename.ext
so the following code I use to change filenames to include the "\\?\":
import os
import platform
def full_path_windows(filepath):
if platform.system() == 'Windows':
if filepath[1:3] == ':\\':
return u'\\\\?\\' + os.path.normcase(filepath)
return os.path.normcase(filepath)
I use this for every path to file (or directories), it will return the path with a prefix. The path does not need to exist; so you can use this also before you create a file or directory, to ensure you are not running into the Windows Explorer limitations.
HTH

Resolving mixed slashes from sys.path and os.path.join

I need to resolve a disparity between the separator that sys.path is providing, and the separator that os.path.join is using.
I mimicked this Esri method (Techniques for sharing Python scripts) to make my script portable. It is being used in Windows for now, but will eventually live on a Linux server; I need to let Python determine the appropriate slash.
What they suggest:
# Get the pathname to this script
scriptPath = sys.path[0]
# Get the pathname to the ToolShare folder
toolSharePath = os.path.dirname(scriptPath)
# Now construct pathname to the ToolData folder
toolDataPath = os.path.join(toolSharePath, "ToolData")
print "ToolData folder: " + toolDataPath
But this outputs ToolData folder: C:/gis\ToolData -- and obviously the mixed slashes aren't going to work.
This Question (mixed slashes with os.path.join on windows) includes the basic approach to a solution:
check your external input (the input you apparently do not control the format of) before putting it in os.path.join. This way you make sure that os.path.join does not make bad decisions based on possibly bad input
However, I'm unsure how to ensure that it will work cross-platform. If I use .replace("/","\\") on the sys.path[0] result, that's great for Windows, but isn't that going to cause the same mixed-slash problem once I transition to Unix?
How about using os.path.normpath()?
>>> import os
>>> os.path.normpath(r'c:\my/path\to/something.py')
'c:\\my\\path\\to\\something.py'
Also worth mentioning: the Windows path API doesn't care whether forward or back slashes are used. Usually it's the fault of program that doesn't handle the slashing properly. For example, in python:
with open(r'c:/path/to/my/file.py') as f:
print f.read()
will work.
After reading the documentation and trying a lot of variations:
The os.path.abspath function can "clean" the slashes, so whichever direction slash sys.path[0] decides to use, the slashes will be replaced with the preferred separator.
scriptPath = sys.path[0]
toolDataPath = os.path.join(scriptPath, "ToolData")
Result: C:/gis\ToolData
scriptPath = sys.path[0]
toolSharePath = os.path.abspath(scriptPath)
# or, in one line: toolSharePath = os.path.abspath(sys.path[0])
toolDataPath = os.path.join(toolSharePath, "ToolData")
Result: C:\gis\ToolData
There is an os.sep character in Python, which stores your OS's preferred folder separating character. Perhaps you could perform a manual string join using that?
On Linux:
>>> import os
>>> os.sep
'/'
https://docs.python.org/2/library/os.html#os.sep

Handling slashes in python

I use python to write scripts for Autodesk Maya. Maya is a cross-platform software and internally use forward slash. If I use os.path.join operation on windows it can result paths like this:
e:/Test\\TemplatePicture.jpg
My idea is that as long as I don't use ms-dos commands easier way to join path parts like this:
pathPart1 = "e:"
pathPart2 = "Test"
pathPart3 = "TemplatePicture.jpg"
path = "s%/s%/s%" % (pathPart1, pathPart2, pathPart3)
Is there something that makes it a bad idea?
When you import os, python will create an os.path specific to your platform. On linux its posixpath and on windows its ntpath. When you are working on Maya paths, use posixpath. It will follow linux conventions even on windows. When you need to go native, convert using the realpath for your current system.
import os
import posixpath
maya_path = posixpath.join('a','b','c')
local_path = os.path.realpath(maya_path)
I don't see any problems with this.
In fact there is a related question here.
To summarize the discussion within the provided link - you either let python handle file paths or you do it all yourself

No such file or directory python in linux only (coming from windows)

I have the same exact directory structure within a folder in Windows & in Linux (Debian) - where the script is along the static + dataoutput folders
How come the following code works fine in Windows, but gives a no such file or directory path error in linux?
#app.route('/_getdataoutputfilelisting')
def getdataoutputfilelisting():
listoffilesindataouput = getfiles('static/dataoutput')
return jsonify(listoffiles = listoffilesindataouput)
def getfiles(dirpath):
a = [s for s in os.listdir(dirpath)
if os.path.isfile(os.path.join(dirpath, s))]
a.sort(key=lambda s: os.path.getmtime(os.path.join(dirpath, s)))
a.reverse()
return a
Is there a way to make it universal such that it works in both OSs?
Thanks
I would try changing the string your passing into getfiles.
Try this instead:
os.path.join(os.getcwd(),'static','dataoutput')
I cannot verify that it'll work in Windows, but that works in Linux; whereas 'static/dataoutput' won't.
Windows and Linux have different path separators. For Windows, parts of the path are separated by a backward slash (\), while on Linux it's a forward slash (/). You can use os.path.join('static', 'dataoutput') as #mcneo suggested (even without getcwd()) or create the path on your own with os.path.sep.
getfiles(os.path.join('static', 'dataoutput'))
# or...
getfiles('static' + os.path.sep + 'dataoutput')
Technically you can also check for the platform and put the correct slash in, but that's not recommended. I'm adding it here to better demonstrate the problem, but you should use os.path.
if sys.platform == 'win32':
getfiles('static\\dataoutput')
else:
getfiles('static/dataoutput')
It probably has something to do with the way DOS and Unix treat things like newlines and returns differently. Try dos2unix tool to convert the file.
SYNOPSIS
dos2unix [options] [FILE ...] [-n INFILE OUTFILE ...]
unix2dos [options] [FILE ...] [-n INFILE OUTFILE ...]

Categories