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

The python file contains this code:
def choice(keys="yn"):
    import os
    import time
    o = open("choice.txt", "wb")
    i = open("python.txt", "r+")
    return errorlevel

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?


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

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

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