769Views4Replies

Author Options:

read and right a text file through batch in python Answered

Ok so I have a
- batch file named run.choice.bat 
- text file named choice.txt
- text file named python.txt
- and a python file named choice.py

The batch file contains this code:
@echo off
set /p c=<"choice.txt"
echo %c%
choice /c:%c%
echo %errorlevel%
echo %errorlevel% > python.txt
pause

The python file contains this code:
def choice(keys="yn"):
    """Choice.com"""
    import os
    import time
    o = open("choice.txt", "wb")
    o.write(keys);
    time.sleep(1)
    os.system("run.choice.bat")
    i = open("python.txt", "r+")
    errorlevel=int(i.read(2))
    return errorlevel
choice(raw_input("choice("))

my problem is that the batch file wont read choice.txt while running in python, on its own it runs fine.
can somebody help me fix it?

4 Replies

user
mwrenfro (author)2011-03-18

From the Python docs on write:

"file.write(str) -- Write a string to the file. There is no return value. Due to buffering, the string may not actually show up in the file until the flush() or close() method is called."

Select as Best AnswerUndo Best Answer

user
NachoMahma (author)2011-03-11

. Just a guess, but the Python interpreter probably changes the working directory so the the BAT file can't find the data file. Try specifying the complete path to the data file inside the BAT file. Eg, set /p c=<"c:\mydata\choice.txt"

Select as Best AnswerUndo Best Answer

user
FastLearner (author)NachoMahma2011-03-11

Thank you for the quick reply. It didn't work got any other Ideas?

Select as Best AnswerUndo Best Answer