I have read the docs but it doesn’t tell how we enter the file path in open() function.
If file path is:
/opt/myapp/report/sample.txt
or
$MYPATH/report/sample.txt (where $MYPATH=/opt/myapp)
Is it ok to write the statement this way:
f = open('/opt/myapp/report/sample.txt', "r")
or
f = open('$MYPATH/report/sample.txt', "r")
What you want to do here is expand the environment variables in the path, which can be done with
os.path.expandvars():E.g:
Note my use of the
withstatement here, which is the best way to open files, as it ensures they are closed correctly, even when there is an exception, and reads nicely.