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

2
  • It should write msg to the file, where msg is equal to operazione since msg = operazione. Commented Feb 9, 2016 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. Commented Feb 9, 2016 at 17:40

3 Answers 3

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.

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

Comments

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().

8 Comments

I suggest using file.write() rather than redirecting stdout of the print() call to a file.
You can do that. Either is fine. The advantage of print(), however, is that you don't need to induce conversions.
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.
@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.
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().
|
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)

2 Comments

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.
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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.