-1

I am afraid I do not know how to phrase my problem, but this post asks about what I am trying to say. However, I was wondering if this was possible in Python. (once again, sorry for my lack of being able to explain my problem).

I have tried the sys.argv[0] command, but this only gives me the path of the executable, where I want the path of a file opened with the executable.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
SomeMosa
  • 413
  • 5
  • 28
  • 1
    do you mean `sys.argv[1]`? if the way you open the file with the executable is something like `executable_file file_of_interest`, then what you're looking for is `sys.argv[1]`. remember the position index starts from `0` where in this case `0` is the executable itself. – billydh Jan 26 '19 at 02:37
  • Yes! Thank you! I have been searching for the answer for so long! If you want me to mark your answer as correct, please post an answer instead of a comment (but thank you so much!!) – SomeMosa Jan 26 '19 at 02:46

1 Answers1

1

Python executable sees the file it opens as located at position index [1], where the executable is at index [0]. So, to get the path of the file opened by your python executable, you should use sys.argv[1].

billydh
  • 975
  • 11
  • 27
  • Note that the second argument is not necessarily the name of a file to be opened by the program. It could be anything. – eesiraed Mar 04 '19 at 01:46
  • Yes that is fair enough, however, OP specifically talks about the file to be opened, nothing about other type of argument was mentioned. So, my answer is specifically to address what OP was after. – billydh Mar 04 '19 at 01:54
  • Just making sure that this won't confuse others. – eesiraed Mar 04 '19 at 01:55