1176Views4Replies
read and right a text file through batch in python
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?
Discussions
9 years ago
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."
9 years ago
. 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"
Answer 9 years ago
Thank you for the quick reply. It didn't work got any other Ideas?
Answer 9 years ago
. No. :(