jstack jstack - 1 year ago 96
Python Question

How to format the beginning a loop correctly?

this is a very simple question (ironic cause I can't answer it :/). I have a program which I designed both for myself and my colleague to use, with all the data being stored in a Dropbox folder that is shared between both of us. This is what I have:

file_location = glob.glob('/Users/epsuser/Dropbox/Argo/Data/*.nc')

This works perfectly fine for me, as my computer has the
directories. However, I want to set up the loop so that it work both for me and him. I tried all of these:

file_location = glob.glob('/../../Dropbox/Argo/Data/*.nc')
file_location = glob.glob('/../Dropbox/Argo/Data/*.nc')
file_location = glob.glob('../Dropbox/Argo/Data/*.nc')
file_location = glob.glob('../../Dropbox/Argo/Data/*.nc')

But none of them are picking up any files. How can I fix this? Cheers!

Answer Source

You can get a directory relative to a user's home (called ~ in the function call) using os.path.expanduser(). In your case, the line would be

file_location = glob.glob(os.path.expanduser('~/Dropbox/Argo/Data/*.nc'))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download