0

I put a python script to my /usr/local/bin to use as a command in bash.

I want to use my bash working directory, but I only get the current directory where the script is executed.

Is there a way to use the current directory as a command does? E.g. with convert2ogg (as python script in /usr/local/bin)

$ cd ~/Music
$ convert2ogg *.mp3
Rutrus
  • 1,367
  • 15
  • 27
  • that should be the default - i.e. a python program, like any other program, will act on files as globbed on the command line by the shell unless it both ignores files listed on the command line AND either changes directory itself or opens a specific directory to read the files list....or, as your script does, prepends the dirname of ARGV[0] to any files before opening them. this is a question with a "don't do that, then" answer. – cas Apr 25 '16 at 06:45
  • 1
    Possible duplicate of [Find current directory and file's directory](http://stackoverflow.com/questions/5137497/find-current-directory-and-files-directory) – miradulo Apr 25 '16 at 14:50

1 Answers1

2
import os,sys

print("CWD: "+os.getcwd())
print("Script: "+sys.argv[0])
print(".EXE: "+os.path.dirname(sys.executable))
print("Script dir: "+ os.path.realpath(os.path.dirname(sys.argv[0])))
pathname, scriptname = os.path.split(sys.argv[0])
print("Relative script dir: "+pathname)
print("Script dir: "+ os.path.abspath(pathname))

This code was very clear for me. os.path was not the solution for all my problems ;) I look for an answer, and seems anybody ask it before. The first line with os.getcwd() solved my problem.

Rutrus
  • 1,367
  • 15
  • 27