Suppose python code is executed in not known by prior windows directory say 'main' , and wherever code is installed when it runs it needs to access to directory 'main/2091/data.txt' .
how should I use open(location) function? what should be location ?
Edit :
I found that below simple code will work..does it have any disadvantages ?
file="\2091\sample.txt"
path=os.getcwd()+file
fp=open(path,'r+');
r"\2091\sample.txt". Or escape them like"\\2091\\sample.txt"(but that is annoying). Also, 2) you are using getcwd() which is the path you were in when you execute the script. I thought you wanted relative to the script location (but now am wondering). And 3), always useos.pathfunctions for manipulating paths. Your path joining line should beos.path.join(os.getcwd(), file)4) the ; is pointless - Russwith open(path, 'r+') as fp:. See here for the best explanation ofwithstatements I've seen. - Russos.path.abspathto get easly the full path of the relative path to open. final statement looks like this:os.path.abspath('./2091/sample.txt')- OPMendeavor