2

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!

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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%

2 Comments

this one works but the goal of my script is to plot all 18 files together and this one plots each of the files separately ... do you know why?
I added another example. this will create a list in %files% like " filename1.txt filename11.txt"
0

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

2 Comments

this one works as well but also treats each file separately and not together as it should. It should plot 18 files into one plot instead of plotting each to its own figure
@user3041107: Updated. Sorry, i missunderstood it.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.