tags:

views:

91

answers:

4

I'm using a script that someone else wrote in python. It's executed from the command line with 3 arguments.

example: "python script.py 1111 2222 3333"

It does it's thing and works perfectly. The results are NOT saved though, and I would really like to pipe the output to a text file. Can I simply use similar dos commands to accomplish this? ie "...333 > output.txt"

I don't really want to post the script here if possible since it's not really my work.

A: 

yes, if you are in a typical unixy shell, that will work exactly as you hope.

If you want to modify the script to write to a file instead of to the stdout, you can read about how to use file()

TokenMacGuy
A: 

That seems to work fine for me in the DOS shell.

jkerian
+1  A: 
f = open('/path/to/file','w')
f.write(string, '\n') # ... etc.

Should be simple enough to add something like that to the script, just in case you'd rather not have to use the shell to pipe output each time.

arbn
I already tried this and I keep getting indentation errors.. I can write code in java, html, css, and some php but I have 0 python experience. And when I define f it usually said "invalid syntax" with a triangle pointing at f. Does it have to be defined at the top. I'm using a windows command shell. This is truely the method I'd like to employ.
Bob
A: 

Redirection works fine both in unix-y shells and in Windows' cmd.exe (which I suspect is what you're calling "the DOS window"... unless you're managing to run Python on Windows '95 or something!-).

$ python script.py 1111 2222 3333 >output.txt

where the $ is not something you type, but rather stands for "whatever prompt your shell / command window is giving you". Just to be totally unambiguous, what you do type at said prompt to get redirection is just:

python script.py 1111 2222 3333 >output.txt

just like what you type now (without redirection) is

python script.py 1111 2222 3333
Alex Martelli