![]() |
| Writing into 2 text files from the same function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Writing into 2 text files from the same function (/thread-37830.html) |
Writing into 2 text files from the same function - paul18fr - Jul-27-2022 Hi The following snippet seems to work, but I'm thinking I've been "lucky" and I'm wondering if it's the good way ? indeed neither f1 nor f2 have been passed as argument. Thanks to hghtlight what's the good pratice Paul import os Path = str(os.getcwd()) n = 10 ##### def MyFunction(n): f1.write(f"Square value of {n} is {n**2}\n") f2.write(f"Cubic value of {n} is {n**3}\n") with open(Path + '/file1.txt', 'w') as f1, open(Path + '/file2.txt', 'w') as f2 : for i in range(1, n): MyFunction(i) RE: Writing into 2 text files from the same function - paul18fr - Jul-27-2022 In a certain way the answer may have ever been in the remark; but still wondering if it's thet correct way import os Path = str(os.getcwd()) n = 10 ##### def MyFunction(f, g, n): f.write(f"Square value of {n} is {n**2}\n") g.write(f"Cubic value of {n} is {n**3}\n") with open(Path + '/file1.txt', 'w') as f1, open(Path + '/file2.txt', 'w') as f2 : for i in range(1, n): print(f"i = {i}") MyFunction(f1, f2, i) RE: Writing into 2 text files from the same function - Larz60+ - Jul-27-2022 what are you trying to write to? single letter object names leave no clue, and in general, are poor writing style, exceptions perhaps when writing MCU code with very limited memory. At any rate, your syntax is fine. RE: Writing into 2 text files from the same function - deanhystad - Jul-27-2022 MyFunction(f, g, n) is a bad function. 1. The name is meaningless. 2. The argument names are meaningless. 3. It makes your code harder to read than not using a function. 4. The function has no purpose that is easily described in a few short sentences. 5. What it does do is silly. When you write a function it should have a well defined purpose. Let's say the purpose of your function is: Write a function "table" to a file. This would be my first attempt: def write_function_table(function, values, filename): with open(filename, "w") as file: for value in values: print(value, function(value), file=file) write_function_table(lambda x: x**2, range(1, 11), "squares.txt")This produces a file that looks like this: It would be nice if the file "looked pretty", so I add a format string.def write_function_table(function, values, filename, fmt_str="{inp}, {out}"): with open(filename, "w") as file: for value in values: print(fmt_str.format(inp=value, out=function(value)), file=file) write_function_table(lambda x: x**2, range(1, 11), "squares.txt", "square {inp} = {out}")This produces:It would be nice if I could use this function and have it write to stdout instead of a file, or maybe write multiple tables to one file.def write_function_table(function, values, fmt_str="{inp}, {out}", file=None): for value in values: print(fmt_str.format(inp=value, out=function(value)), file=file) values = range(1, 11) with open("function tables.txt", "w") as file: print("Squares", file=file) write_function_table(lambda x: x**2, values, "{inp} = {out}", file) print("\nSquare roots", file=file) write_function_table(lambda x: x**0.5, values, "{inp} = {out}", file) write_function_table(lambda x: x**3, values, "{inp}**3 = {out}")This creates a function tables.txt file that looks like this:And also writes this to stdout.And to make it easier for others to use my function I would provide documentation.def write_function_table(function, values, fmt_str="{x}, {fx}", file=None): """Write a "function table" to the file. function : The function to evaluate for x values : Iterable of x values fmt_str : String compatible with format() command. Use "x" for x and "fx" for function(x) file : File to write results. If None, writes to stdout """ for x in values: print(fmt_str.format(x=x, fx=function(x)), file=file) values = range(1, 11) with open("squares.txt", "w") as file: write_function_table(lambda x: x**2, values, "square {x} = {fx}", file) write_function_table(lambda x: x**3, values, "{x}**3 = {fx}")Now not only can I use it, but others can use it too. And when my users ask me to add generating square root tables or combining multiple tables in one file it will be easy for me to quickly modify my program to add those capabilities. RE: Writing into 2 text files from the same function - ndc85430 - Jul-28-2022 Naming is key, because functions are a tool for abstraction, as well as reuse. |