运行带参数的python函数

时间:2017-06-01 21:54:03

标签: python

使用ncinfo命令,我可以从csh(例如<body> <div id="backgroundnav"> <nav class="navbar"> <ul> <li><a href="#">Home</a></li> <li><a href="#about">About</a></li> <li><a href="#projects">Projects</a></li> <li><a href="contact.html">Contact</a></li> </ul> </nav> </div> <div id="box1"> <div id="wrap"> <h1>Peter Nguyen</h1> <hr> <h1>Web Developer</h1> </div> </div> <div id="color"> <div id="content"> <section id="about"> <h1 id="about">About Me</h1> <hr id="hr1"> <p id="aboutleft">My names Peter and learning how to code and persuing it in education was one of the best decisions I have made. When I am not at work coding and solving complex web application issues, I am at home learning the latest and greatest frameworks. One of the best feelings is knowing that you solved an issue. I am extremly easy to get along with and easy to work with which would make me a great addition to any team. Web development is more than just a hobby and career path for me its a passion and a way for me to get creative and think outside of the box! </p> <p id="aboutright">What else do I do besides code? I enjoy barbequing, lifting weights, being outdoors. I am also secretely a nerd so I enjoy playing video games, and reading books. Did I also mention that I enjoy playing ping pong? I was born in California and lated move out to Arizona as a young kid growing up. As a kid I had a very high interest in computers and always wondered about the person who created every component a computer has. I beilive that is why to this day I am so intrigued by development. </p> </section> </div> <div id="left"> <h1>Place holder content for future case projects</h1> <input type="text" id="case1"> <input type="text" id="case1x"> </div> <div id="right"> <h1>Place holcer content for future case projects</h1> <input type="number" name=""> </div> </div> <div id="projects"> <h1>Projects</h1> <figure id="link" class="align"> <a href="https://puu.sh/vIKzj/a98ecf7f9e.png"><img src="image.jpg" alt="Golden Gate Bridge" width="500" height="350"></a> <figcaption> Guessing game created with JavaScript </figcaption> </figure> <figure class="align"> <a href="https://puu.sh/vHIlA/29a21ac2de.png"><img src="image.jpg" alt="Golden Gate Bridge" width="500" height="350"></a> <figcaption> Halong Bay kayak </figcaption> </figure> <figure class="align"> <a href="http://halongbaycruises.com.au/wp-content/uploads/2015/01/Plitvice-lakes-chain.jpg"><img src="image.jpg" alt="Golden Gate Bridge" width="500" height="350"></a> <figcaption> Halong Bay Waterfalls </figcaption> </figure> <figure class="align"> <a href="http://images.surprisinghalong.com/uploads//2013/11/surprising-cave-ha-long-bay.jpg"><img src="image.jpg" alt="Golden Gate Bridge" width="500" height="350"></a> <figcaption> Halong Bay Cave </figcaption> </figure> </div>)查看netcdf文件的内容。 ncinfo命令实际上调​​用了python函数 here。我的问题是:如何直接从python shell使用ncinfo函数?

我试过了

ncinfo test.nc

但我无法弄清楚如何传递文件名&#39; test.nc&#39;到功能。 有什么想法吗?

编辑:根据评论,我认为现在的问题是

如何假冒&#39; python shell中的一个参数,用于提供一个使用from netCDF4.utils import ncinfo ncinfo() getopt.getopt接受参数的函数?

1 个答案:

答案 0 :(得分:2)

正如Alan Leuthardjez在评论中指出的那样,人们可以在调用函数之前手动修改sys.argv(并不知道它是可变的)。所以一个有效的解决方案是

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