How do i save output to a file in python?
The most obvious way to do this would be to print to a file object: Show
However, redirecting stdout also works for me. It is probably fine for a one-off script such as this:
Since Python 3.4 there's a simple context manager available to do this in the standard library:
Redirecting externally from the shell itself is another option, and often preferable:
Other questions: What is the first filename in your script? I don't see it initialized. My first guess is that glob doesn't find any bamfiles, and therefore the for loop doesn't run. Check that the folder exists, and print out bamfiles in your script. Also, use os.path.join and os.path.basename to manipulate paths and filenames.
mkrieger1 15.7k4 gold badges46 silver badges57 bronze badges answered Aug 22, 2011 at 20:00
Gringo SuaveGringo Suave 28.4k6 gold badges84 silver badges74 bronze badges 12 You can redirect print with the
In most cases, you're better off just writing to the file normally.
or, if you have several items you want to write with spaces between, like
mkrieger1 15.7k4 gold badges46 silver badges57 bronze badges answered Aug 22, 2011 at 19:56
3 Python 2 or Python 3 API reference:
Since file object normally contains Write/Overwrite to File
Write/Append to File
mkrieger1 15.7k4 gold badges46 silver badges57 bronze badges answered Jul 4, 2016 at 13:45
YeoYeo 10.9k6 gold badges62 silver badges89 bronze badges 2 This works perfectly:
Now the hello will be written to the test.txt file. Make sure to close the
Andy K 4,79210 gold badges48 silver badges80 bronze badges answered Jun 30, 2015 at 12:15
1 Don't use print, use loggingYou can change With A simple exampleImport
If you want to print to stdout:
If you want to also write to a file (if you only want to write to a file skip the last section):
Then, wherever you would use
To learn more about using more advanced answered Jan 1, 2018 at 20:54
jpyamsjpyams 3,5857 gold badges35 silver badges59 bronze badges 2 The easiest solution isn't through python; its through the shell. From the first line of your file (
to run the file, use
where you replace One important thing that needs to be mentioned here is that "script.py" needs to be made executable for So before running
answered Aug 22, 2011 at 20:24
Aaron DufourAaron Dufour 16.9k1 gold badge48 silver badges68 bronze badges 3 If you are using Linux I suggest you to use the
If you don't want to change anything in the code, I think this might be the best possible solution. You can also implement logger but you need do some changes in the code.
tripleee 164k27 gold badges244 silver badges296 bronze badges answered Jan 23, 2018 at 15:51
yunusyunus 2,1871 gold badge13 silver badges12 bronze badges 0 You may not like this answer, but I think it's the RIGHT one. Don't change your stdout destination unless it's absolutely necessary (maybe you're using a library that only outputs to stdout??? clearly not the case here). I think as a good habit you should prepare your data ahead of time as a string, then open your file and write the whole thing at once. This is because input/output operations are the longer you have a file handle open, the more likely an error is to occur with this file (file lock error, i/o error, etc). Just doing it all in one operation leaves no question for when it might have gone wrong. Here's an example:
And then when you're all done collecting your "data lines" one line per list item, you can join them with some
However if you have lots of data to write, you could write it one piece at a time. I don't think it's relevant to your application but here's the alternative:
answered Aug 22, 2011 at 20:20
machine yearningmachine yearning 9,4815 gold badges36 silver badges51 bronze badges 8 If redirecting To make it even easier, I made a version utilizing
contextmanagers for a succinct generalized calling syntax using the
To use it, you just do the following (derived from Suave's example):
It's useful for selectively redirecting answered Jan 6, 2019 at 4:48
GrahamGraham 2,5153 gold badges15 silver badges29 bronze badges 2 I am able to crack this using the following method. It will use this print function instead of builtin print function and save the content to a file.
answered Sep 18, 2021 at 19:51
Arjun G PerambraArjun G Perambra 1,9891 gold badge16 silver badges16 bronze badges 1 Here's another method I've used for printing to a file/log... Modify the built-in print function so that it logs to a file in the temp directory with the current time stamp, as well as print to stdout. The only real advantage to doing this within a script is not having to go and modify existing print statements.
Copy original print function to new variable
Overwrite existing print function
display file
remove file
answered Jan 25 at 15:28
TCB919TCB919 1136 bronze badges Changing the value of sys.stdout does change the destination of all calls to print. If you use an alternative way to change the destination of print, you will get the same result. Your bug is somewhere else:
answered Aug 22, 2011 at 20:05
JeromeJerome 2,32114 silver badges24 bronze badges In python 3, you can reassign
Note that the print from
Personally, I'd
prefer sidestepping the requirement to use the
answered Dec 8, 2020 at 19:04
ShawnShawn 6115 silver badges10 bronze badges Something to extend print function for loops
answered Jul 16, 2017 at 19:34
1 How do you store output in Python?Step 1: Import libraries StringIO and sys. ... . Step 2: Keep stdout in temporary variable. ... . Step 3: Capture standard output using a StringIO object. ... . Step 4: Assign Standard Output Stream to StringIO object. ... . Step 5: Print to the standard output. ... . Step 6: Clean up by redirecting stdout to Python shell.. How do I save output as PDF in Python?Approach:. Import the class FPDF from module fpdf.. Add a page.. Set the font.. Insert a cell and provide the text.. Save the pdf with “. pdf” extension.. How do you write data into a file in Python?You can write to a file in Python using the open() function . You must specify either “w” or “a” as a parameter to write to a file. “w” overwrites the existing content of a file. “a” appends content to a file.
How do you save a text file as output?In the command, change "YOUR-COMMAND" with your command and "c:\PATH\TO\FOLDER\OUTPUT. txt" with the path and file name to store the output. In the command, change "YOUR-COMMAND" with your command and "c:\PATH\TO\FOLDER\OUTPUT. txt" with the path and filename to store and view the output.
|