python script error when writing to another file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



python script error when writing to another file



i am trying to write a python script to get all the content in one file to be written to another file . but i am getting the below error . the code i am using is :


#!/user/bin/env python

with open('log.txt', 'r') as file:

line = file.read()

with open('/etc/snort/rules/test.rules' , 'w') as f2:

f2.write(line)



the error i get is :



in line 7


with open('/etc/snort/rules/test.rules' , 'w') as f2:

***IndentationError : expected an indented block***





Python is indentation sensitive, everythng in your with statement should have a one-level indentation
– Olivier Melançon
Aug 11 at 23:41





Then you should indent the line f2.write(line) as well as line = file.read(). Indentation is usually 4 spaces per level of (Hint: if a line ends in a colon :, then at least one line underneath it needs to be indented).
– N Chauhan
Aug 11 at 23:42



f2.write(line)


line = file.read()


:





Hi , i am new in using python , i am not sure what should i do to fix the error?
– Sara
Aug 11 at 23:44




1 Answer
1


#!/user/bin/env python

with open('log.txt', 'r') as file:
line = file.read()
#### <-- the line above has 4 spaces worth of indentation.
with open('/etc/snort/rules/test.rules' , 'w') as f2:
f2.write(line)



I indented the lines under the 'with' statements by 4 spaces





@Sara mark this answer as correct if it has solved your issue. It makes the solution clearer to others and gives @N Chauhan credit for helping. Thanks :)
– Reece Mercer
Aug 12 at 1:22






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered