How can I code a Python script that accepts a file as an argument and prints its full path?
E.g.
~/fileFinder$ ls
./ ../ fileFinder.py test.md
~/fileFinder$ py fileFinder.py test.md
/Users/me/fileFinder/test.md
~/fileFinder$ py fileFinder.py /Users/me/Documents/index.html
/Users/me/Documents/index.html
So, it should find the absolute path of relative files, test.md
, and also absolute path of files given via an absolute path /Users/me/Downloads/example.txt
.
How can I make a script like above?
Ok, I found an answer:
import os
import sys
relative_path = sys.argv[1]
if os.path.exists(relative_path):
print(os.path.abspath(relative_path))
else:
print("Cannot find " + relative_path)
exit(1)