0

I am implementing a logging process which append to the log file. I want to check, if the log file exist then append to more lines to the file. If not then create the new file then append but i keep getting error saying: No such file or directory

        try:
            f = open(os.path.join(
                BASE_DIR, '/app/logs/log-' + current_date + '.csv'), "a+")
            f.write(message + "\n")
        except IOError:
            f = open(os.path.join(
                BASE_DIR, '/app/logs/log-' + current_date + '.csv'), "w+")
            f.write(message + "\n")
        finally:
            f.close()

What mistake am i making here?

============ Update

This code is working :

        try
            f = open('log-' + current_date + '.csv'), "a+")
            f.write(message + "\n")
        except IOError:
            f = open('log-' + current_date + '.csv'), "w+")
            f.write(message + "\n")
        finally:
            f.close()

if i open the file like this, its working. But as soon as i add the path there. Its just keep saying no file or directory.

=============== Update

Never mind, it has been working.I forgot to rebuild my docker image to see the results. :DD. So the problem is the incorrect path.

5
  • 2
    When using os.path.join, if an argument starts with a /, all the previous arguments will be ignored. Try printing os.path.join( BASE_DIR, '/app/logs/log-' + current_date + '.csv'). Commented Dec 6, 2019 at 18:05
  • 2
    couldn't you just open the file with "a+"? Commented Dec 6, 2019 at 18:06
  • 1
    a+: Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. Commented Dec 6, 2019 at 18:06
  • @FPSedin's comment probably fixes your error. My comment was just generally on the fact that you don't need a try/except block. Commented Dec 6, 2019 at 18:42
  • @user1558604 Thanks. It has been working the whole time. I just forgot to rebuild my docker image :D. But i agree. I dont think we need the try catch block here. Thanks again Commented Dec 6, 2019 at 18:44

2 Answers 2

2

The output of os.path.join will be /app/logs/log-<current_date>.csv. This is not what you want. Remove the leading / from that second argument and it will work as you want. This happens because you passed it an absolute path as the second input. See os.path.join documentation for an explanation.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I agree. Thats the problem. I fixed it and its working now. I just forgot to rebuild my docker image :D. stupid me.
0

Why not do something like this:

import os

check = os.path.isfile("file.txt")

with open("file.txt", "a+") as f:
    if not check:
        f.write("oi")
    else:
        f.write("oi again")

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.