简体   繁体   English

如何使用正则表达式在python的命令行中将多个文件作为参数传递?

[英]how do i pass multiple files as arguments in command line for python using regular expressions?

I'm not able to do this 我做不到

ptags.py *.py ptags.py * .py

or 要么

python *.py python * .py

i'm getting an error saying "Cannot open file named *.py" 我收到一条错误消息:“无法打开名为* .py的文件”

but i'm able to open all the python files in vim using this command 但是我可以使用此命令在vim中打开所有python文件

vim *.py vim * .py

python 2.7 in windows 7 command prompt Windows 7命令提示符中的python 2.7

I'm not sure what the sensible behaviour would be for the interpreter to do when presented with more than one source file - If you just want it to execute all .py files in the current directory, then you should be explicit. 我不确定当呈现多个源文件时,解释器将采取什么明智的行为-如果只希望它执行当前目录中的所有.py文件,则应该明确。

(Note - haven't done Windows command lines in a while, so this is OTTOMH): (注意-一段时间没有执行Windows命令行,所以这是OTTOMH):

for %i in (*.py) do python %i

On the other hand - if you're only executing a single source file, and want to expand out matches - then look at glob or os.listdir and co. 另一方面-如果您只执行一个源文件,并且想扩展匹配项-那么请查看globos.listdir和co。 and inside your code loop over that... something like: 并在您的代码循环内...类似:

import sys
from glob import glob

in_pattern = sys.argv[1]
for filenames in glob(in_pattern):
    print filename

You cannot using standard Windows cmd shell. 您不能使用标准Windows cmd shell。 You can use something like bash from Cygwin, maybe PowerShell. 您可以使用来自Cygwin的bash之类的功能,也可以使用PowerShell。

If you want to open *.py from application like vim but in Python, then you can use glob module. 如果要从vim应用程序中打开*.py ,但要在Python中打开,则可以使用glob模块。

暂无
暂无

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

相关问题 如何使用命令行在pytest中传递多个参数? - How to pass multiple arguments in pytest using command line? 如何在调试模式下将命令行参数从 VS 传递给 Python? - How do I pass command line arguments to Python from VS in Debug mode? 如何为从批处理文件运行的python脚本传递变量命令行参数? - How do I pass variable command line arguments for a python script, that runs from a batch file? 如何在python中使用正则表达式形成单独的块? - How do I form separate blocks using regular expressions in python? 如何使用Python中的正则表达式在页面中搜索文本? - How do I search for text in a page using regular expressions in Python? 如何在python脚本功能内部的sql查询中传递多个命令行参数或系统参数? - How to pass multiple command line arguments or system arguments in sql query which is inside the function of python script? 如何使用python命令行将多个文件(.nii格式)作为输入文件传递? - How to pass multiple files(.nii format) as input file using python command line? 如何在 Windows 命令行中使用参数运行 Python 脚本 - How do I run Python script using arguments in windows command line 如何从python中的多个正则表达式生成多个句子 - How do I generate multiple sentences from multiple regular expressions in python 如何将 arguments 从命令行传递到 python 脚本以读取和更新 json 文件中存在的某些键的值 - How do I pass arguments from command line to the python script to read and update values of certain keys present in a json file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM