I am new to Python and I need to write a simple data processing script. I made a very simple script that just takes a file name from the program arguments from the command line and just prints the value of the first argument:
import sys
if __name__ == '__main__':
fileName = sys.argv[1]
print "File name is %s" % fileName
Then I run the program: myProgram.py ~/datadir/file.txt
Since nothing tells python that the argument is actually a path, I am surprise that infers it itself and resolves the ~ into a fully qualified path and the program outputs:
File name is /Users/<my_username>/datadir/file.txt
However, I am able to work around that by wrapping the command line argument in quotes:
myProgram.py "~/datadir/file.txt"
File name is ~/datadir/file.txt
Since I am in the process of learning Python, I was wondering is someone could explain what drives this implicit resolution. E.g. does it automatically assume anything starting with ~ is a path?