1

Today I managed to run my first Python script ever. I'm a newb, on Windows 7 machine.

When I run python.exe and enter following (Python is installed in C:/Python27)

import os
os.chdir('C:\\Pye\\')
from decoder import *
decode("12345")

I get the desired result in the python command prompt window so the code works fine. Then I tried to output those results to a text file, just so I don't have to copy-paste it all manually in the prompt window. After a bit of Googling (again, I'm kinda guessing what I'm doing here) I came up with this;

I wrote "a.py" script in the C:/Pye directory, and it looked like this;

from decoder import *
decode("12345")

And then I wrote a 01.py file that looked like this;

import subprocess
with open("result.txt", "w+") as output:
subprocess.call(["python", "c:/Pye/a.py"], stdout=output);

I see the result.txt gets created in the directory, but 0 bytes. Same happens if I already make an empty result.txt and execute the 01.py (I use Python Launcher).

Any ideas where am I screwing things up?

2
  • what is out output of a.py. Does decode print anything? Commented Feb 24, 2016 at 18:09
  • Yep it does, it's a code from git actually that I'm using, but I shortned it just so I don't bog the window with the full syntax. So when I'm using the commands in python.exe, it outputs text, and if I want to save it, I have to copy-paste, which is tedious. So I was wondering what could I do to make it automatically do that task. I tried running the python a.py>result.txt but I would get "Access is denied". Commented Feb 24, 2016 at 18:11

2 Answers 2

1

You didn't print anything in a.py. Change it to this:

from decoder import *
print(decode("12345"))

In the Python shell, it prints it automatically; but the Python shell is just a helper. In a file, you have to tell it explicitly.

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

4 Comments

I'm curious myself. Does it matter if the decode function calls print somewhere in its body?
Calling the function will print everything that the function says to print, but what the function returns will not be printed unless either what calls the function, or the function itself prints it.
@zondo thanks! This works. I get a output I want in txt file. Now, one more bonus question if I may - I have many of the decode lines to process, is there a way to instruct the python to process them one after another, for example decode("12345"), decode("abcde") obviously this is just my presumption of possible code :)
You could create a list of the things you want to decode: decodelist = ["12345", "abcde"]. Then use a for loop: for d in decodelist: print(decode(d)).
0

When you run python and enter commands, it prints to standard out (the console by default) because you're using the shell. What is printed in the python shell is just a representation of what object is returned by that line of code. It's not actually equivalent to explicitly calling print.

When you run python with a file argument, it executes that script, line by line, without printing any variables to stdout unless you explicitly call "print()" or write directly to stdout.

Consider changing your script to use the print statement.:

print(decode("12345"))

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.