0

Today, I was testing my old python script, it was about fetching some details from an API and write then in a file. Until my last test it was working perfectly fine but today when I executed the script it worked, I mean no error at all but it neither write nor created any file. The API is returning complete data - I tested on it terminal, then I created another test.py file to check if file write statements are working, so the result was - they were not working. I don't know what is causing the issue, it also ain't giving any error.

This is my sample TEST.PY file

filename = "domain.log"

with open(filename, 'a') as domain_file:
    domain_file.write("HELLO\n")
    domain_file.write("ANOTHER HELLO\n")

Thank you

2
  • This works perfectly fine for me. Maybe you are not checking the right place? It created the file and wrote in it. Then running it again, it simply appends. Commented Apr 16, 2020 at 11:52
  • I don't know what was causing the issue, it is now working for me too. Thanks anyways Commented Apr 16, 2020 at 11:58

2 Answers 2

1

Using 'a' on the open call to open the file in append mode (as shown in your code) should work just fine.

I don't think your issue is on the Python side. The next thing to check are your directory permissions:

$ ls -al domain.log
-rw-r--r--  1 taylor  staff  60 Apr 16 07:57 domain.log

Here's my output after running your code a few times:

$ cat domain.log
HELLO
ANOTHER HELLO
HELLO
ANOTHER HELLO
HELLO
ANOTHER HELLO
Sign up to request clarification or add additional context in comments.

4 Comments

"a" works - if a file does not exist, it gets created.whats being written to it, is appended.
Then I think your code is fine. Check that you have permission to write to disk with ls -al domain.log.
Thank you - updated my answer with sample output for verification.
thank you so much, I tested the script again, it is now working, I don't know what was causing the issue but it is now working
0

It may be related to file permission or its directory. Use ls -la to see file and folder permissions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.