Make a Path Work Both on Linux and Windows

Make a path work both on linux and Windows

In Linux, the path separator is /. In Windows, it is either \ or /. So just use forward slashes and you will be fine.

APPLICATION_PATH . '/logs/app.log'

How to specify the path to access a file in both windows and linux uniquely using slash seperator

This works fine when i used file.separator.

public static final String QWD = File.separator +"qark-master" + File.separator +"qark" + File.separator +"qarkMain.py";
public static final String MANIFESTPATH=File.separator +"apktool"+ File.separator +"AndroidManifest.xml";

How to use / (directory separator) in both Linux and Windows in Python?

Use os.path.join().
Example: os.path.join(pathfile,"output","log.txt").

In your code that would be: rootTree.write(os.path.join(pathfile,"output","log.txt"))

Path.equals behaves different on Windows and Linux

Path separator is different for Windows and Linux.

For Windows is \

For Linux is /

Safe way in java of building paths that work on both evironments is

Path filePath = Paths.get("path1", "path2");

In your case you use a String to form a Path. So in Windows

 String path2 = "path1/file1.jpg";
Paths.get(path2) -> results in "path1\file1.jpg"

It converts the separator / to a windows separator \

After that conversion both path1 and path2 are the same

Now when you run in Linux the following code

      String path1 = "path1\\file1.jpg"; -> this will not be ok for linux and also will not try to convert it to / as the first \ is an escape character
String path2 = "path1/file1.jpg"; -> this will be ok for linux /
System.out.println(Paths.get(path1));
System.out.println(Paths.get(path2));
System.out.println(Paths.get(path1).equals(Paths.get(path2)));

File path names for Windows and Linux

Normally, when specifying file paths on Windows, you would use backslashes. However, in Java, and many other places outside the Windows world, backslashes are the escape character, so you have to double them up. In Java, Windows paths often look like this: String WinDir = "C:\\trash\\blah\\blah";. Forward slashes, on the other hand, do not need to be doubled up and work on both Windows and Unix. There is no harm in having double forward slashes. They do nothing to the path and just take up space (// is equivalent to /./). It looks like someone just did a relpace of all backslashes into forward slashes. You can remove them. In Java, there is a field called File.separator (a String) and File.separatorChar (a char), that provide you with the correct separator (/ or \), depending on your platform. It may be better to use that in some cases: String WinDir = "C:" + File.separator + "trash" + File.separator + "blah" + File.separator + "blah";

I'm writing C# code that has to run on windows and linux (under mono). Easy way to handle file paths on both?

Use Path.PathSeparator. \ on Windows, / on Unix.

If you're looking to combine directory names, you can use Path.Combine.

To get the root directory (i.e. / or C:\, you can use Path.GetPathRoot(Directory.GetCurrentDirectory())).

More info in the Path docs.

Relative path seperators for windows and linux in python

You can use os.path.normpath as shown below. It can adjust to both Linux and Windows as per the system and it also resolves the path into the shortest possible path to the destination. Ex: /home/foo/../A will be resolved to /home/A.

Usage:

import os.path

# Path
path = './home//user/Documents'


# Normalize the specified path
# using os.path.normpath() method
norm_path = os.path.normpath(path)

# Print the normalized path
print(norm_path)
# prints home/user/Documents in Linux
# prints home\user\Documents in Windows

For more info read this article: https://www.geeksforgeeks.org/python-os-path-normpath-method/

How to pass any directory format (windows, linux, mac) to a function and use it os independently (python3)

If you're using python > 3.4 you can use pathlib and it does exactly that. You create a Path object from your path and it handles everything for you.

On older versions of Python, as long as you use os.path for any path manipulations you need, and use the Unix format for any hard-coded constants together with os.path.normpath, it should work on any os.



Related Topics



Leave a reply



Submit