1

I want the user to be able to type paths as arguments for my program
The progeram should work over all OS...
Until now i used x=input("...") and parsed this x as my path
But in Unix it is more complicated, in case i run my program from ~/a/c/b and i want the argument to be directory 'a', i should type ../../ ?
What should i write in Unix in order to get correct path... ?
Or is there another module to use in order to get user paths in the template above ?

Thank you !

SagiLow
  • 5,721
  • 9
  • 60
  • 115
  • What's your question? What have you tried? – Eric Mar 03 '13 at 19:53
  • i'm asking how to get these paths as full paths... should i use input() ? raw_input() is there better modules for input paths handling ? – SagiLow Mar 03 '13 at 19:54
  • Do you mean you want to convert relative paths to absolute paths? You can use [`os.path.abspath`](http://docs.python.org/2/library/os.path.html#os.path.abspath). – Bakuriu Mar 03 '13 at 19:57
  • in python 2, "input" will treat `4/2` as 2. You should never use input in python 2. Why do you need to do any handling at all? You're trying to solve a problem that you've never had. – Eric Mar 03 '13 at 19:58
  • I think the absolute path is the answer. Thank you ! – SagiLow Mar 03 '13 at 20:11

1 Answers1

2

You should not use relative paths such as ../../ because you may never know from where the user runs your application. If your program is in ~/a/c/b and you want the argument to be directory a, then you should just use ~/a as the argument.

As for writing the same code to run on all operating systems, you should use things such as os.path.join and os.path.normpath. More info on how to handle paths in python here.

  • When i type any '/' in Unix i get SyntaxError, it doesn't happen in Windows, only in Unix, it happens when i use input(), when i use raw_input() it doesn't happen in Unix but i get an error in Windows – SagiLow Mar 03 '13 at 20:29
  • Why are you talking about `input` and `raw_input`? If you want to use command line arguments, you don`t have to use any of those two functions. Read this SO question to see how to use command line arguments in python http://stackoverflow.com/questions/1009860/command-line-arguments-in-python –  Mar 03 '13 at 20:39
  • When the user doesn't provide arguments or invalid arguments i would like the user to type the arguments into CMD, that's why i need input()/raw_input() – SagiLow Mar 03 '13 at 20:57