简体   繁体   English

将目录作为命令行参数传递给 python 脚本

[英]Pass directory to python script as command line argument

I have a few sub-directories that live in the same folder as my python script.我有一些子目录与我的 python 脚本位于同一文件夹中。 I am trying to run the script inside one of the sub-directories.我正在尝试在其中一个子目录中运行脚本。

example:例子:

$ script.py subdirectory

Is there a way to pass in the sub directory at command line有没有办法在命令行传入子目录

import argparse

parser = argparse.ArgumentParser()

parser.add_argument("-subdir", "--subdirectory", help="enter subdirectory name")

params = parser.parse_args()

subdir = params.subdirectory

## and continue your code using subdir as the used subdirectory name

So when you want to run:所以当你想运行时:

script.py -subdir SUBDIRECTORY_NAME_HERE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将目录作为命令行参数传递给python脚本 - Passing directory to python script as command line argument 将列表作为命令行参数传递给Python脚本 - Pass a list to a Python script as a command line argument 将列表 object 作为命令行参数传递给 python 脚本 - pass a list object as a command line argument to a python script 将变量从python脚本传递给C程序(命令行参数) - Pass variable from python script to C program(command line argument) python命令行参数并将其作为用户名和密码在脚本中传递 - python command line argument and pass it as username and password in script 在python中使用管道将字符串列表传递给脚本中的命令行参数 - Pass a list of strings to a command line argument within a script with pipe in python 如何将字典作为命令行参数传递给 Python 脚本? - How to pass dictionary as command line argument to Python script? 如何将列表作为命令行参数传递给 python 脚本 - how to pass a list as a command line argument to a python script 如何从 docker run 命令将命令行参数传递给 python 脚本? - How to pass command line argument to python script from docker run command? 如果将目录路径作为命令行参数传递给 python,则执行哪个 python 脚本? - Which python script is executed if directory path is passed as command line argument to python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM