使用 python argparse 进行递归

发布于 2024-10-04 04:33:25 字数 1369 浏览 3 评论 0原文

我正在使用 argparse 编写递归程序。唯一需要的参数是要操作的一个或多个文件。当我递归调用它时,我不需要文件名(因为我将调用一个新目录),但我需要选项。问题是 argparse 允许 python programname.py -options arg FILENAME FILENAME 和 python programname.py FILENAME FILENAME -options arg 。我可以煞费苦心地寻找“-”并用大量的 if 语句来解决它,但我觉得应该有更好的方法。

不确定这是否重要,但这是我的 argparse 声明:

 parser = argparse.ArgumentParser(description='Personal upload script. (defaults to ' + user + '@' + server + directory + ')')
 parser.add_argument('files', nargs="+", help='file(s) to upload')
 parser.add_argument('-s', metavar='example.com', default=server, help='server to upload to')
 parser.add_argument('-u', metavar='username', default=user, help='ftp username')
 parser.add_argument('-p', metavar='password', default=password, help='ftp password')
 parser.add_argument('-d', metavar='example/', default=directory, help='directory to place file in')
 parser.add_argument('-n', metavar='myfile.txt', help='name to save file as')
 parser.add_argument('-c', metavar='###', help='chmod upload')
 parser.add_argument('-l', action='store_true', help='print out new url(s)')
 parser.add_argument('-r', action='store_true', help='recursive')
 parser.add_argument('-F', action='store_true', help='force (overwrite files / create non-existing dirs)')
 parser.add_argument('-v', action='store_true', help='verbose')
 args = parser.parse_args()

非常感谢!

I'm writing a recursive program using argparse. The only required argument is a file (or files) to act on. When I call it recursively, I don't need the filenames (as I'll be calling on a new directory), but i need the options. the problem is that argparse allows both python programname.py -options arg FILENAME FILENAME and python programname.py FILENAME FILENAME -options arg. I could painstakingly search for a '-' and work it out with a ton of if statements, but i feel like there should be a better way.

not sure that it matters, but here are my argparse declarations:

 parser = argparse.ArgumentParser(description='Personal upload script. (defaults to ' + user + '@' + server + directory + ')')
 parser.add_argument('files', nargs="+", help='file(s) to upload')
 parser.add_argument('-s', metavar='example.com', default=server, help='server to upload to')
 parser.add_argument('-u', metavar='username', default=user, help='ftp username')
 parser.add_argument('-p', metavar='password', default=password, help='ftp password')
 parser.add_argument('-d', metavar='example/', default=directory, help='directory to place file in')
 parser.add_argument('-n', metavar='myfile.txt', help='name to save file as')
 parser.add_argument('-c', metavar='###', help='chmod upload')
 parser.add_argument('-l', action='store_true', help='print out new url(s)')
 parser.add_argument('-r', action='store_true', help='recursive')
 parser.add_argument('-F', action='store_true', help='force (overwrite files / create non-existing dirs)')
 parser.add_argument('-v', action='store_true', help='verbose')
 args = parser.parse_args()

thanks so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沐歌 2024-10-11 04:33:25

你只是让自己的生活变得困难。你不是让程序递归,而是让函数递归。使程序递归是耗尽内存并通常压垮系统的绝佳方法。

重写您的应用程序,以便递归工作仅限于调用自身的函数,而不是应用程序的另一个实例。

或者,更好的是,完全消除递归性。看起来您只是在遍历目录树。没有理由递归地这样做。事实上,Python 有一个库函数可以为您遍历目录树。请参阅os.walk

You're just making life difficult for yourself. You don't make programs recursive, you make functions recursive. Making a program recursive is an excellent way to run out of memory and generally overwhelm a system.

Rewrite your application so that the recursive work is confined to a function that calls itself, instead of another instance of your application.

Or, better, eliminate recursiveness entirely. It looks like you're just iterating through a directory tree. There's no reason to do that recursively. In fact, Python has a library function that will take care of walking a directory tree for you. See os.walk.

梦里寻她 2024-10-11 04:33:25

您可能不应该进行进程级递归。也就是说,我认为 argparse 对“--”的处理可能会提供您正在寻找的工具(如果我理解正确的话)。在 argparse 文档中搜索字符串“--”。

You probably shouldn't be doing process-level recursion. That said, I think that argparse's treatment of "--" might provide the tool you're looking for (if I understand correctly). Search the argparse doc for the string "--".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文