0

i'm trying to create a def for log file and this is my script:

import sys
import time
path= 'pc_path'
file = path + '\\' + 'test.txt'

files = open(file, 'w')
files.close()

def log(msg):
    time = time.time()
    filess = open(file, 'a')
    filess.write(msg)
    filess.close()

val = 10
val1 = 32
try:
    operazione = val + val1
    print('ok')
    print(operazione)
    msg = operazione
    log(msg)
except:
    sys.exit()

the script create a txt file but does not write a def() function into txt Thanks

albert
  • 8,027
  • 10
  • 48
  • 84
user5858205
  • 27
  • 1
  • 5
  • It should write `msg` to the file, where `msg` is equal to `operazione` since `msg = operazione`. – albert Feb 09 '16 at 17:11
  • If any of the answers given addresses your question, please mark them as such so that the thread is closed and the question appears as answered on the board. Otherwise, please elaborate. – Pouria Feb 09 '16 at 17:40

3 Answers3

1

You are trying to write an integer to a file; the file.write() method only accepts strings.

Convert to a string first:

filess.write(str(msg))

You really should not use a blanket except handler in your code; you really do't want to play Pokemon and catch them all. By doing so you are missing valuable error information. See Why is "except: pass" a bad programming practice?

Rather than roll your own, you could just use the logging module.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

You open a file, and then you close it. You don't write anything in it though, because the function write() fails should anything but a variable of type str() be passed to it! Here is an alternative to write() with fewer restrictions:

with open('file_name.txt', 'w') as file:

    print(variable_you_want_written, file=file, end='\n')

print() function has fewer restrictions and is a better option here as it automatically formats the inputs it receives as a str(), regardless of the initial type(), and excluding functions, instances and generator objects, which would be displayed as, for instance, something like this: <function <lambda> at 0x1089b70d0>.

Additionally, be aware that the visual format to which they are converted (and subsequently displayed or written in a file) may not necessarily be to your liking, so I suggest you do experiment with the results and see what works best in a particular situation.

Click here for Python 3 docs on print().

Pouria
  • 1,081
  • 9
  • 24
  • I suggest using `file.write()` rather than redirecting stdout of the `print()` call to a file. – albert Feb 09 '16 at 17:14
  • You can do that. Either is fine. The advantage of `print()`, however, is that you don't need to induce conversions. – Pouria Feb 09 '16 at 17:15
  • They do (try to) write something in it. They even use `'a'` append mode. But calling `file.write()` with anything but a string is going to fail. – Martijn Pieters Feb 09 '16 at 17:18
  • @MartijnPieters That's right. It does fail. What I meant by not writing was that nothing is actually going through to the file, as the function fails. I phrased it badly, and I will modify. – Pouria Feb 09 '16 at 17:20
  • Right, `print()` does accept any type and converts it to a string. You don't need to add `end='\n'` though, that's the default. You may want to add an explicit explanation as to *why* `print()` works better here than using `file.write()`. – Martijn Pieters Feb 09 '16 at 17:26
  • @MartijnPieters Sure. I'll add that too. Although it's best if they actually read the docs. `\n` is an old habit, plus, I intentionally put it there so that they'll know of it, and that they can change it if they need to. We have discussions everyday, ey? – Pouria Feb 09 '16 at 17:30
  • Which is why I always try to link to documenation in my answers. :-) – Martijn Pieters Feb 09 '16 at 17:33
  • Ok. I will do that too. But it's gonna be the last modification! ;) – Pouria Feb 09 '16 at 17:36
0
  1. Rename the variable time to something like current_time to avoid conflicts with the module time.

  2. You passed msg, which is an integer, into the method write. You should convert it to a string first: msg = str(operazione)

Dinever
  • 690
  • 5
  • 13
  • 1
    I would suggest doing `msg = str(msg)` directly in the `log` function which would make this function more versatile since casting a string as a string does not change anything. – albert Feb 09 '16 at 17:19
  • These are all very glorious, but they do not address the issue directly. Plus, why would you want to explicitly convert your variable when you can do that implicitly through `print()` via the interpreter, which is way faster (as it is implemented in C) and considerably less tedious? I'm really asking, cause I keep thinking I'm missing a point here, and I don't like that! – Pouria Feb 09 '16 at 18:00