How to open every file in a folder

Os You can list all files in the current directory using os.listdir: import os for filename in os.listdir(os.getcwd()): with open(os.path.join(os.getcwd(), filename), ‘r’) as f: # open in readonly mode # do your stuff Glob Or you can list only some files, depending on the file pattern using the glob module: import os, glob for filename … Read more

Why is printing to stdout so slow? Can it be sped up?

How can it be that writing to physical disk is WAY faster than writing to the “screen” (presumably an all-RAM op), and is effectively as fast as simply dumping to the garbage with /dev/null? Congratulations, you have just discovered the importance of I/O buffering. ๐Ÿ™‚ The disk appears to be faster, because it is highly … Read more

Piping both stdout and stderr in bash?

(Note that &>>file appends to a file while &> would redirect and overwrite a previously existing file.) To combine stdout and stderr you would redirect the latter to the former using 1>&2. This redirects stdout (file descriptor 1) to stderr (file descriptor 2), e.g.: $ { echo “stdout”; echo “stderr” 1>&2; } | grep -v … Read more

How to redirect the output of a PowerShell to a file during its execution

Maybe Start-Transcript would work for you. First stop it if it’s already running, then start it, and stop it when done. $ErrorActionPreference=”SilentlyContinue” Stop-Transcript | out-null $ErrorActionPreference = “Continue” Start-Transcript -path C:\output.txt -append # Do some stuff Stop-Transcript You can also have this running while working on stuff and have it saving your command line sessions … Read more

Command to get nth line of STDOUT

Using sed, just for variety: ls -l | sed -n 2p Using this alternative, which looks more efficient since it stops reading the input when the required line is printed, may generate a SIGPIPE in the feeding process, which may in turn generate an unwanted error message: ls -l | sed -n -e ‘2{p;q}’ I’ve … Read more

multiple prints on the same line in Python

Python 3 Solution The print() function accepts an end parameter which defaults to \n (new line). Setting it to an empty string prevents it from issuing a new line at the end of the line. def install_xxx(): print(“Installing XXX… “, end=””, flush=True) install_xxx() print(“[DONE]”) Python 2 Solution Putting a comma on the end of the … Read more

Confused about stdin, stdout and stderr?

Standard input – this is the file handle that your process reads to get information from you. Standard output – your process writes conventional output to this file handle. Standard error – your process writes diagnostic output to this file handle. That’s about as dumbed-down as I can make it ๐Ÿ™‚ Of course, that’s mostly … Read more

How to pipe stdout while keeping it on screen ? (and not to a output file)

Here is a solution that works at on any Unix / Linux implementation, assuming it cares to follow the POSIX standard. It works on some non Unix environments like cygwin too. echo ‘ee’ | tee /dev/tty | foo Reference: The Open Group Base Specifications Issue 7 IEEE Std 1003.1, 2013 Edition, ยง10.1: /dev/tty Associated with … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)