简体   繁体   English

从Windows cmd运行多个文件的python脚本

[英]running python script for multiple files from windows cmd

I am trying to run python script from windows cmd. 我正在尝试从Windows cmd运行python脚本。 When I run it under linux I put 当我在linux下运行它时

python myscript.py filename??.txt

it goes through files with numbers from filename01.txt to filename18.txt and it works. 它会处理文件编号从filename01.txt到filename18.txt的文件,并且可以正常工作。

I tried to run it from cmd like 我试图从cmd运行它

python myscript.py filename*.txt

or 要么

python myscript.py filename**.txt

but it didnt work. 但是没有用。 If I tried the script on one single file in windows cmd it works. 如果我在Windows cmd中的一个文件上尝试了该脚本,它将起作用。

Do you have any clue where the problem could be? 您是否有问题的根源? Thanks! 谢谢!

Unix shell convert file path pattern to actual files, then pass the result to the program. Unix shell将文件路径模式转换为实际文件,然后将结果传递给程序。 ( python myscript.py ) python myscript.py

But in Windows cmd, this does not happen. 但是在Windows cmd中,不会发生这种情况。

See glob.glob if you want get file list that match the pattern. 如果要获取与模式匹配的文件列表,请参见glob.glob

Those wildcards are expanded at "shell (ie bash) level" before running your python script. 在运行python脚本之前,这些通配符将在“ shell(即bash)级别”进行扩展。 So the problem doesn't reside in python, but in the "shell" that you are using on Windows. 因此,问题不在于python,而在于您在Windows上使用的“ shell”中。 Probably you cloud try PowerShell for Windows or bash via CygWin. 可能您尝试通过Windows将PowerShell运用于Windows或通过CygWin进行bash。

try this: 尝试这个:

FOR %X IN (filename*.txt) DO (python myscript.py %X)

Edit , you can create a .bat with this and try it. 编辑 ,您可以创建一个.bat并尝试。

setlocal EnableDelayedExpansion
set files=
FOR %%X IN (filename*.txt) DO set files=!files! %%X
echo %files%
python myscript.py %files%

From batch file 从批处理文件

for %%f in ("filename*.txt") do python myscript.py "%%~nxf"

%%f will get a reference to each of the files. %%f将获得对每个文件的引用。 For each of them execute your script. 对于每个脚本,执行您的脚本。 %%~nxf will expand to name and extension of file. %%~nxf将扩展为文件名和扩展名。

From command line, replace %% with a single % 在命令行中,将%%替换为单个%

EDITED - I missunderstood the problem. 编辑-我误解了这个问题。 Next try. 下次尝试。

In windows, there is no default expansion of wildcard arguments ( see here ). 在Windows中,没有通配符参数的默认扩展(请参阅此处 )。 So, to get the same result you will need a batch file. 因此,要获得相同的结果,您将需要一个批处理文件。 It will concatenate the list of files and pass it to your python script 它将连接文件列表并将其传递给您的python脚本

@echo off

    setlocal enabledelayedexpansion

    set "fileList="
    for %%f in ("*.txt") do set "fileList=!fileList! "%%f""

    python myscript.py !fileList!

    endlocal

For a more reusable code, use something as (script calls are only echoed to screen to show efect of parameters and to avoid unneeded execution, remove when it works as intended) 对于更可重用的代码,请使用以下命令(脚本调用仅回显到屏幕上以显示参数的效果并避免不必要的执行,请按预期的方式删除)

@echo off

    setlocal enableextensions

    call :glob "*.txt" true fileList
    echo python myscript.py %fileList%

    echo.

    call :glob "*.txt" false fileList
    echo python myscript.py %fileList%

    exit /b

:glob pattern useFullPath outputList
    setlocal enabledelayedexpansion
    if /i "%~2"=="true" (set "_name=%%%%~ff") else (set "_name=%%%%~nxf")
    set "_list="
    for %%f in ("%~1") do set "_list=!_list! "%_name%""
    endlocal & if not "%~3"=="" set "%~3=%_list%"

As falsetru notes, on Windows the shell doesn't expand the wildcards for you, so the correct answer is glob.glob() . falsetru ,在Windows上,shell不会为您扩展通配符,因此正确的答案是glob.glob() You should iterate over all the command line arguments and expand each. 您应该遍历所有命令行参数并展开每个参数。 This works fine in Linux/UNIX too, because the expansion of an argument without any wildcards in it (which is what the shell gives you) is the unchanged filename. 这在Linux / UNIX中也可以正常工作,因为参数的扩展名中没有任何通配符(这是Shell给出的),即文件名不变。 So something like this, using lazy evaluation to handle a potentially large number of args: 像这样,使用惰性求值来处理可能大量的args:

from sys import argv
from glob import glob
from itertools import chain, islice

for name in chain.from_iterable(glob(name) for name in islice(argv, 1, None)):
    # do something with each file

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM