Shichu Zhu
Shichu Zhu

Reputation: 309

run a python function with arguments

With the ncinfo command, I can view the contents of a netcdf file from a csh (Eg. ncinfo test.nc). The ncinfo command actually calls the python function here. My question is: How do I use the ncinfo function directly from a python shell?

I tried the

from netCDF4.utils import ncinfo
ncinfo()

but I can't figure out how to pass the filename 'test.nc' to the function. Any idea?

Edit: Based on the comments, I think the question now is

How to 'fake' an argument inside a python shell to feed a function which use getopt.getopt and sys.argv to accept arguments?

Upvotes: 0

Views: 164

Answers (1)

Shichu Zhu
Shichu Zhu

Reputation: 309

As Alan Leuthard and jez point out in the comments, one can manually modify sys.argv (didn't know it's mutable) before calling the function. So a working solution is

from netCDF4.utils import ncinfo
backup = sys.argv
sys.argv = ['','test.nc']
ncinfo()
sys.argv = backup

Upvotes: 2

Related Questions