Jul-17-2023, 01:19 PM
I have an input text file from which i am selecting certain lines which contains the specified strings. And then i remove replace and delete certain lines from the text file.I was working on the code in my local machine Spyder(Python 3.9) and it was working fine.But when i put the same code in GNU/Linux machine with Python 2.9 , its showing syntax error. I am not able to figure out the exact issue.Could you please help me out here. I am a beginner in Python.Giving below the code and the error i am facing
#!/usr/bin/python
import os
import re
import sys
strings = ["/opt","rm -rf","***"] # Search for strings and pick only those lines
with open(r"Input.txt", "r" ) as data:
for deltaa in data:
if any(x in deltaa for x in strings): #Check if any item in strings exits in deltaa
with open("tmp1.txt", "a") as f:
print (deltaa, file=f)
with open('tmp1.txt', 'r') as f:
s = f.read().replace('cloud', '') #Replace the word cloud with blank
with open('tmp2.txt', 'w') as f:
f.write(s)
inf = open("tmp2.txt")
stripped_lines = [l.lstrip() for l in inf.readlines()] # Delete the blank space
inf.close()
# write the new, stripped lines to a file
outf = open("tmp3.txt", "w")
outf.write("".join(stripped_lines))
outf.close()
file = open("tmp3.txt", "r")
lines = file.readlines()
lines = lines[1:] # Skip first line
new_lines = []
for line in lines:
if "remove" not in line.strip(): #Delete the lines that contain remove
new_lines.append(line)
file.close()
file = open("tmp4.txt", "w")
file.writelines(new_lines)
file.close()Error:File "./Script.py", line 13
print (deltaa, file=f)
^
SyntaxError: invalid syntax
