3

This is a stupid question, and I know it is, but for some reason I can't find any useful tutorials for running python from windows command prompt so I'll have to ask you guys. I have a script I need to run on all files starting FY*.txt or WS*.txt in one directory. I've tried going to the directory through command prompt and doing

for file in FY*.txt; do python my_script.py

which just informs me that 'file' is unexpected at this time. I've also tried

python my_script.py FY1.txt FY2.txt FY3.txt

with

import sys
inputfilenames=sys.argv[1:27]

for name in inputfilenames:
    datafile=open(name,'r')

as the way I open my files in the python script itself. This seems to only run the script on one file, rather than all of them.

I apologise for my ignorance, I really have no clue how to use command prompt to run python things. As well as answers, if anyone has any tutorial recommendations I would be very, very grateful.

5
  • If you change your line 2 of a script to inputfilenames=sys.argv[1:] and then print inputfilenames, will you see a list? Commented Aug 9, 2012 at 16:14
  • It isn't printing a list, should it? Commented Aug 9, 2012 at 16:19
  • yep, basically it should. what does it print? Commented Aug 9, 2012 at 16:19
  • Oh, sorry, I'm being an idiot (I didn't save the changes to the file). It is printing a list! Yay!! Is that good? Commented Aug 9, 2012 at 16:21
  • 2
    for /? may be useful :) for %f in (FY*.txt) do python my_script.pyq %f Commented Aug 9, 2012 at 16:24

1 Answer 1

5

I'm not quite certain what that initial example is supposed to be, but to do that from the standard Windows command prompt, you could use something like this:

for %G in (FY*.txt); do python my_script.py %G

If you do something like this, you'll need something like the following in your code:

with open(sys.argv[1], 'r') as f:
    do_something_with(f)

Alternatively, you could look into using the fileinput module to take a list of files as in your second example and process them. That is, inside your script you'd have something like:

for line in fileinput.input():
    do_something_with(line)

Or you could make the wildcard expression an argument and use the glob module, so you could run:

python my_script.py FY*.txt

And then in your script do something like:

for file in glob.glob(sys.argv[1]):
     with open(file, 'r') as f:
         do_something_to(f)

The glob could be run over multiple arguments:

for files in([glob.glob(arg) for arg in sys.argv[1:]]):
    for file in files:
        with open(file, 'r') as f:
            do_something_to(f)

which would allow you to execute:

python my_script FY*.txt WS*.txt
Sign up to request clarification or add additional context in comments.

7 Comments

while doing for %G in (FY*.txt); do python my_script.py %G it seems to get stuck on one file (it prints [FY1.txt] and doesnt move on). Also, what should I put inside the script so it knows the input is coming from command prompt?
@Snaaa: The for statement is going to launch your Python script once for each file, assuming the Python script ends as some point. I'm not really clear on what your script is doing, so it's hard to be too specific, but see the edit I made above.
@Snaaa: In particular, does your script need to know the name of the file it's processing? Does it need to be aware of changes in file context? Are you just handling lines in the file or running with different logic?
it's just extracting some data from each file, fiddling with it, and writing it to a file, so that the data from all files will be together in one file. For all intents and purposes the files are identical, they're just lists of times and dates
@Snaaa: Is your code arranged so you have some sort of main function that can be called with a line or open file handle as a parameter (what I've called do_something_to() in my examples above)?
|

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.