Abdullah
Abdullah

Reputation: 305

Unable to write files in python

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

Upvotes: 0

Views: 168

Answers (2)

Taylor D. Edmiston
Taylor D. Edmiston

Reputation: 13016

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

Upvotes: 1

Matin Zadeh Dolatabad
Matin Zadeh Dolatabad

Reputation: 1017

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

Upvotes: 0

Related Questions