I have a hobby CNC machine with a strangely written software. In order to use it, I need to take my file that is outputted by a CAD software and add a new line (CRLF) after every ";". I wanted to make a python script that would do this instead, but I can't get it to work. Can anyone point me in the right direction?
import os
import sys
if not (len(sys.argv) == 2 and os.path.isfile(sys.argv[1])):
print(__doc__)
sys.exit(1)
file_in = open(sys.argv[1], "r+")
currentChar = file_in.read(1)
i = 0
while not currentChar:
if (currentChar == ";"):
file_in.seek(i)
file_in.write("\n")
currentChar = file_in.read(1)
i += 1
file_in.close()
As mentioned by CristiFati's comment, you should read the file, close it and then open it for writing:
import sys
file_in = open(sys.argv[1],'r')
content = file_in.read()
file_in.close()
file_out = open(sys.argv[1],'w')
file_out.write(content.replace(';',';\n'))
file_out.close()
Optionally you can change the output filename to be something different so that you don't overwrite your original file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.