gerowen
November 2nd, 2006, 04:18 AM
I've got a little bit of experience in writing python programs that take information from the user and write a Linux bash script to implement the information collected. I recently tried to write a program to write a .bat file in Windows and where the "\" would be it inserts a space instead of the \ and the letter following it. For example if I wanted to write a file to delete a text file named temp.txt located in C:\, here is what I would write:
import commands
infile=open("delfile.bat", "w")#Opens the file
infile.write("del C:\temp.txt")#Writes the desired contents to the file
infile.close()#Closes the file
commands.getstatusoutput(delfile.bat)#Executes the file
That bat file executes because I see the DOS window flash for a second like normal, but the file remains in C and the contents of the .bat file would be:
del C: emp.txt
I think this is because python regards backslashes as symbols, like how \n will insert a new line, does somebody know how I could get it to keep the \ and actually write it into the file?
import commands
infile=open("delfile.bat", "w")#Opens the file
infile.write("del C:\temp.txt")#Writes the desired contents to the file
infile.close()#Closes the file
commands.getstatusoutput(delfile.bat)#Executes the file
That bat file executes because I see the DOS window flash for a second like normal, but the file remains in C and the contents of the .bat file would be:
del C: emp.txt
I think this is because python regards backslashes as symbols, like how \n will insert a new line, does somebody know how I could get it to keep the \ and actually write it into the file?