EzDevInfo.com

path interview questions

Top path frequently asked interview questions

How to get the path of a running JAR file?

My code runs inside a JAR file, say foo.jar, and I need to know, in the code, in which folder the running foo.jar is.

So, if foo.jar is in C:\FOO\, I want to get that path no matter what my current working directory is.


Source: (StackOverflow)

Homebrew wants me to amend my PATH. No clue how

Trying to install ruby 1.9.3, read that I need to install homebrew first. Ran brew doctor, and it's giving me a bunch of warnings. One of which is:

Warning: /usr/bin occurs before /usr/local/bin This means that system-provided programs will be used instead of those provided by Homebrew. The following tools exist at both paths:

easy_install
easy_install-2.6

Consider amending your PATH so that /usr/local/bin is ahead of /usr/bin in your PATH.

How does one do what it's asking here?


Source: (StackOverflow)

Advertisements

Path.Combine for URLs?

Path.Combine is handy, but is there a similar function in the .NET framework for URLs?

I'm looking for syntax like this:

Url.Combine("http://MyUrl.com/", "/Images/Image.jpg")

which would return:

"http://MyUrl.com/Images/Image.jpg"


Source: (StackOverflow)

How to construct a relative path in Java from two absolute paths (or URLs)?

Given two absolute paths, e.g.

/var/data/stuff/xyz.dat
/var/data

How can one create a relative path that uses the second path as its base? In the example above, the result should be: ./stuff/xyz.dat


Source: (StackOverflow)

Find full path of python interpreter?

How do I find the full path of the currently running Python interpreter from within the currently executing Python script?


Source: (StackOverflow)

Convert file path to a file URI?

Does the .NET Framework have any methods for converting a path (e.g. "C:\whatever.txt") into a file URI (e.g. "file:///C:/whatever.txt")?

The System.Uri class has the reverse (from a file URI to absolute path), but nothing as far as I can find for converting to a file URI.

Also, this is not an ASP.NET application.


Source: (StackOverflow)

How do I get the path of the current executed file in python?

This may seem like a newbie question but it is not. Some common approaches don't work in all cases:

sys.argv[0]

This means using path = os.path.abspath(os.path.dirname(sys.argv[0])) but this does not work if you are running from another Python script in another directory, and this can happen in real life.

__file__

This means using path = os.path.abspath(os.path.dirname(__file__)) but I found that this doesn't work:

  • py2exe doesn't have a __file__ attribute, but there is a workaround
  • when you run from IDLE with execute() there is no __file__ attribute
  • OS X 10.6 where I get NameError: global name '__file__' is not defined

Related questions with incomplete answers:

I'm looking for a generic solution, one that would work in all above use cases.

Update

Here is the result of a testcase:

output of python a.py (on Windows)

a.py: __file__= a.py
a.py: os.getcwd()= C:\zzz

b.py: sys.argv[0]= a.py
b.py: __file__= a.py
b.py: os.getcwd()= C:\zzz

a.py

#! /usr/bin/env python
import os, sys

print "a.py: sys.argv[0]=", sys.argv[0]
print "a.py: __file__=", __file__
print "a.py: os.getcwd()=", os.getcwd()
print

execfile("subdir/b.py")

subdir/b.py

#! /usr/bin/env python
import os, sys

print "b.py: sys.argv[0]=", sys.argv[0]
print "b.py: __file__=", __file__
print "b.py: os.getcwd()=", os.getcwd()
print

tree

C:.
|   a.py
\---subdir
        b.py

Source: (StackOverflow)

Get real path from URI, Android KitKat new storage access framework [duplicate]

This question already has an answer here:

Before new gallery access in KitKat I got my real path in sdcard with this method

public String getPath(Uri uri) {
   String[] projection = { MediaStore.Images.Media.DATA };
   Cursor cursor = managedQuery(uri, projection, null, null, null);
   startManagingCursor(cursor);
   int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
   cursor.moveToFirst();
 return cursor.getString(column_index);
}

Now, the Intent.ACTION_GET_CONTENT return different data:

Before:

content://media/external/images/media/62

Now:

content://com.android.providers.media.documents/document/image:62

How could I manage to obtain the real path in sdcard?


Source: (StackOverflow)

python, path of script [duplicate]

Duplicate:
In Python, how do I get the path and name of the file that is currently executing?

How do i get the path of a the python script i am running in? I was doing dirname(sys.argv[0]), however on Mac I only get the filename - not the full path as I do on Windows.

No matter where my application is launched from, I want to open files that are relative to my script file(s).


Source: (StackOverflow)

How to set current working directory to the directory of the script?

I'm writing a bash script. I need the current working directory to always be the directory that the script is located in.

The default behavior is that the current working directory in the script is that of the shell from which I run it, but I do not want this behavior.


Source: (StackOverflow)

Test if executable exists in Python?

In python, is there a portable and simple way to test if an executable program exists?

By simple I mean something like the 'which' command which would be just perfect. I don't want to search PATH manually or something involving trying to execute it with Popen & al and see if it fails (that's what I'm doing now, but imagine it's launchmissiles)


Source: (StackOverflow)

Combine paths in Java

Is there a Java equivalent for System.IO.Path.Combine() in C#/.NET? Or any code to accomplish this?

This static method combines one or more strings into a path.


Source: (StackOverflow)

Why does the 260 character path length limit exist in Windows?

I have come up against this problem a few times at inopportune moments:

  • trying to work on open source Java projects with deep paths
  • Storing deep Fitnesse wiki trees in source control
  • An error trying to use Bazaar to import my source control tree

Why does this limit exist?

Why hasn't it been removed yet?

How do you cope with the path limit? ... and no, switching to linux or Mac OS X is not a valid answer to this question ;)


Source: (StackOverflow)

bash/fish command to print absolute path to a file

Question: is there a simple sh/bash/zsh/fish/... command to print the absolute path of whichever file I feed it?

Usage case: I'm in directory /a/b and I'd like to print the full path to file c on the command-line so that I can easily paste it into another program: /a/b/c. Simple, yet a little program to do this could probably save me 5 or so seconds when it comes to handling long paths, which in the end adds up. So it surprises me that I can't find a standard utility to do this — is there really none?

Here's a sample implementation, abspath.py:

#!/usr/bin/python
# Author: Diggory Hardy <diggory.hardy@gmail.com>
# Licence: public domain
# Purpose: print the absolute path of all input paths

import sys
import os.path
if len(sys.argv)>1:
    for i in range(1,len(sys.argv)):
        print os.path.abspath( sys.argv[i] )
    sys.exit(0)
else:
    print >> sys.stderr, "Usage: ",sys.argv[0]," PATH."
    sys.exit(1)

Source: (StackOverflow)

Android SDK manager won't open

So I installed the android sdk for Windows:

http://developer.android.com/sdk/index.html (the installation link)

And ran into the path variable problem. So I fixed that by changing "PATH" in enviroment variables to include where my java.exe file is located from the JDK.

But now when I open the android sdk manager, a cmd-like screen just briefly flashes on for half a second then disappears. I have no idea what's going on and how to get this thing working.


Source: (StackOverflow)