user3041107
user3041107

Reputation: 123

running python script for multiple files from windows cmd

I am trying to run python script from windows cmd. When I run it under linux I put

python myscript.py filename??.txt

it goes through files with numbers from filename01.txt to filename18.txt and it works.

I tried to run it from cmd like

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.

Do you have any clue where the problem could be? Thanks!

Upvotes: 2

Views: 3235

Answers (5)

kindall
kindall

Reputation: 184201

As falsetru notes, on Windows the shell doesn't expand the wildcards for you, so the correct answer is 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. So something like this, using lazy evaluation to handle a potentially large number of 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

Upvotes: 0

MC ND
MC ND

Reputation: 70923

From batch file

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

%%f will get a reference to each of the files. For each of them execute your script. %%~nxf will expand to name and extension of file.

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). 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

@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%"

Upvotes: 0

malexander
malexander

Reputation: 4680

try this:

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

Edit, you can create a .bat with this and try it.

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

Upvotes: 0

smeso
smeso

Reputation: 4295

Those wildcards are expanded at "shell (i.e. bash) level" before running your python script. So the problem doesn't reside in python, but in the "shell" that you are using on Windows. Probably you cloud try PowerShell for Windows or bash via CygWin.

Upvotes: 0

falsetru
falsetru

Reputation: 369094

Unix shell convert file path pattern to actual files, then pass the result to the program. (python myscript.py)

But in Windows cmd, this does not happen.

See glob.glob if you want get file list that match the pattern.

Upvotes: 1

Related Questions