Errors When Trying to Save Command Line Output to a File

Errors when trying to save command line output to a file

You will want to specify the encoding of your string before you print it:

print unicode(hex(self.bb.bb.start + self.offset)).encode('utf-8')
print unicode(self.pos_instruction, self.ins.get_name()).encode('utf-8')
print unicode(self.ins.show_buff( self.bb.bb.start + self.offset )).encode('utf-8')

The reason this works is because python automatically encodes your string correctly (in your case utf-8) when printing to the terminal (it detects that the terminal uses utf-8).

When you are redirecting your output to a file instead, Python has no information about what encoding it should use and it defaults to ascii instead (which is causing your error).

As a general rule of thumb, make sure you always encode your string before printing to make print work in all environments.

The best method may be to define your own print method for this:

def myprint(unicodestr): 
print unicodestr.encode('utf-8')

If you want to avoid the above and make printing with utf-8 encoding the default you can do

import sys
import codecs
sys.stdout=codecs.getwriter('utf-8')(sys.stdout)

Beware of this approach! Some third-party libraries may depend on the default encoding being ascii and break. Note that this whole mess has been resolved in Python 3 (which defaults to UTF-8 encoding)

Bash command output not saving to text file or variable

Since "permission denied" is typically considered an error, is the output being routed to stderr instead of stdout? If so, you need to use 2> temp.txt or > temp.txt 2>&1.

More information:

On many systems, program output is broken up into multiple streams, most commonly stdout (standard output) and stderr (standard error). When you use >, that only redirects stdout, but 2> can be used to redirect stderr. (This is useful if you want normal output and errors to go to two different files.)

The syntax 2>&1 means "take all output on stderr and redirect it to stdout", so your file would contain output from both streams.

Redirecting command-line output to keep error messages from showing in the command window

How about this:

DIR %MYDIR%\tmp > nul 2>&1

"> nul" means to redirect standard output to the file nul (the bit bucket).

"2>" is used to redirect standard error (descriptor 2). So "2>&1" is used to redirect standard error to means that standard output (descriptor 1 -- so "> null and 1> null are be the same). Alternatively you could use "2> nul".

Redirect console output to a file with errors python

The logging module is what I went with. It works way better.

logging.info('message') was pretty much all I did.

CMD - redirect cd command output into new file

You have a delayed expansion problem (which causes %cd% to be the same value for each run).

But you don't really need it when you use conditional execution:

for /f "delims=" %x in (paths.txt) do cd "%x" 2>nul && echo %x >> "C:\users\vb3\results.txt"

Writing to a file line for line is slow (as the file has to be opened, read until the end of the file, written to, and closed for each single line (you probably won't notice it with just a few lines, but with thousands of lines, it is huge (we are speaking of a factor of 1000 and more). Better redirect the whole output in one go (in short: (loop with many echoes here)>file.txt instead of loop with many "echo>>file.txt")

(for /f "delims=" %x in (paths.txt) do cd "%x" 2>nul && echo %x)>"C:\users\vb3\results.txt"

&& serves as "if previous command (cd ...) was successful, then".

If you need the oposite, use || ("if previous command failed, then"):

(for /f "delims=" %x in (paths.txt) do cd "%x" 2>nul || echo %x>"C:\users\vb3\denied.txt")

2>nul redirects any error messages (access is denied) to "nirvana" (suppresses them)

delims= processes the whole line (without it, it would just output the first word). Essential when there are spaces in the file- or folder-names.



Related Topics



Leave a reply



Submit