-3

I am getting a TypeError: object of type file' has no len() I have traced down the issue to the path established upon execution.

What am I missing to correct this error found within the "savePath" deceleration or usage within the "temp = os.path.join(savePath, files)"?

def printTime(time):
    savePath = "C:\Users\Nicholas\Documents"
    files = open("LogInLog.txt", "a")
    temp = os.path.join(savePath, files)
    files.write("A LogIn occured.")
    files.write(time)
    print files.read
    files.close

main()

The whole program is below for reference:

from time import strftime
import os.path

def main():
    getTime()

def getTime():
    time = strftime("%Y-%m-%d %I:%M:%S")
    printTime(time)

def printTime(time):
    savePath = "C:\Users\Nicholas\Documents"
    files = open("LogInLog.txt", "a")
    temp = os.path.join(savePath, files)
    files.write("A LogIn occured.")
    files.write(time)
    print files.read
    files.close

main()
  • 1
    you should refer https://docs.python.org/2/library/logging.html – pigletfly Aug 16 '16 at 02:10
  • "something is going wrong within the `printTime()` function" is not a question, at least say _what_ is going wrong. – Julien Aug 16 '16 at 02:19
  • Sorry edited my question... thought it was self explanatory what I was trying to do... but I have not used the "import os.path" import too much and am having trouble trying to get the file to be saved in the directory within the code – Nicholas Shaffer Aug 16 '16 at 02:39

1 Answers1

1

Here's a working version:

from time import strftime
import os.path

def main():
    getTime()

def getTime():
    time = strftime("%Y-%m-%d %I:%M:%S")
    printTime(time)

def printTime(time):
    savePath = "C:\Users\Nicholas\Documents"
    logFile = "LogInLog.txt"
    files = open(os.path.join(savePath, logFile), "a+")
    openPosition = files.tell()
    files.write("A LogIn occured.")
    files.write(time)
    files.seek(openPosition)
    print(files.read())
    files.close()

if __name__ == '__main__':
    main()

There were a few problems with the code snippet posted in the question:

  1. Two import statements were concatenated together. Each should be on a separate line.

  2. The os.path.join function doesn't work on an open filehandle.

  3. The read() and close() methods were missing parens.

  4. If the intent is to read what is written in append mode, it's necessary to get the current file position via tell() and seek() to that position after writing to the file.

  5. While it's legal to call main() without any conditional check, it's usually best to make sure the module is being called as a script as opposed to being imported.

Lex Scarisbrick
  • 1,540
  • 1
  • 24
  • 31
  • Executed perfectly and just as I wanted it to... and thank you for your breakdown it certainly will help my future of this program. One more thing, because I have found out that .pcf (python compiled files) do not execute correctly upon startup of windows, I have been researching how to use pyinstaller and am having trouble figuring out how to actually run the pyinstaller on my .py file to create a standalone .exe file so windows can run the file without issues. any suggestions? – Nicholas Shaffer Aug 16 '16 at 02:56
  • You're most welcome. I'm not very familiar with the Windows platform, so I can't offer any suggestions for your `pyinstaller` question apart from opening another question here on stack overflow. Good luck! – Lex Scarisbrick Aug 16 '16 at 03:13