stdout

Is it possible to print without using the print function in Python?

♀尐吖头ヾ 提交于 2020-07-03 08:09:14
问题 I wondered whether it is possible to print (for example a string) in Python without the print function. This can be done by a command or by some trick. For example, in C there are printf and puts . Can someone show me a way to print or to deny this possibility? 回答1: sys.stdout.write("hello world\n") 回答2: import sys sys.stdout.write("hello") 回答3: You can use sys.stdout.write() Sometimes I find sys.stdout.write more convenient than print for printing many things to a single line, as I find the

Is it possible to print without using the print function in Python?

一个人想着一个人 提交于 2020-07-03 08:08:19
问题 I wondered whether it is possible to print (for example a string) in Python without the print function. This can be done by a command or by some trick. For example, in C there are printf and puts . Can someone show me a way to print or to deny this possibility? 回答1: sys.stdout.write("hello world\n") 回答2: import sys sys.stdout.write("hello") 回答3: You can use sys.stdout.write() Sometimes I find sys.stdout.write more convenient than print for printing many things to a single line, as I find the

Argparse and ArgumentDefaultsHelpFormatter. Formatting of default values when sys.stdin/stdout are selected as default

空扰寡人 提交于 2020-06-25 09:14:02
问题 I am interested in using the ArgumentDefaultsHelpFormatter class formatter of argparse (my program has several sub-commands). By default, the input and output arguments are set to sys.stdin and sys.stdout respectively. However, the formatting for these two arguments may be a little bit confusing for users (e.g (default: ', mode 'r' at 0x10028e0c0>). Is there a way to specifically and easily change the output format for these two arguments to get something like 'default: STDIN' or 'default:

Argparse and ArgumentDefaultsHelpFormatter. Formatting of default values when sys.stdin/stdout are selected as default

廉价感情. 提交于 2020-06-25 09:13:39
问题 I am interested in using the ArgumentDefaultsHelpFormatter class formatter of argparse (my program has several sub-commands). By default, the input and output arguments are set to sys.stdin and sys.stdout respectively. However, the formatting for these two arguments may be a little bit confusing for users (e.g (default: ', mode 'r' at 0x10028e0c0>). Is there a way to specifically and easily change the output format for these two arguments to get something like 'default: STDIN' or 'default:

Printing a font icon with os.write

不羁岁月 提交于 2020-06-23 14:08:27
问题 With Python 3.8.3 on Windows 10, my terminal, Set with a nerdfont that has icons, shows the thumbs up icon for: import os, sys # For the following attempts print('\uf164') I also get a thumbs up via: sys.stdout.write('\uf164') However, for: os.write(1, '\uf164'.encode('utf8')) I get 3 characters, one for each encoded byte. How do I print the thumbs up using os.write ? 来源: https://stackoverflow.com/questions/62502211/printing-a-font-icon-with-os-write

Printing a font icon with os.write

风流意气都作罢 提交于 2020-06-23 14:07:37
问题 With Python 3.8.3 on Windows 10, my terminal, Set with a nerdfont that has icons, shows the thumbs up icon for: import os, sys # For the following attempts print('\uf164') I also get a thumbs up via: sys.stdout.write('\uf164') However, for: os.write(1, '\uf164'.encode('utf8')) I get 3 characters, one for each encoded byte. How do I print the thumbs up using os.write ? 来源: https://stackoverflow.com/questions/62502211/printing-a-font-icon-with-os-write

Printing a font icon with os.write

心不动则不痛 提交于 2020-06-23 14:07:08
问题 With Python 3.8.3 on Windows 10, my terminal, Set with a nerdfont that has icons, shows the thumbs up icon for: import os, sys # For the following attempts print('\uf164') I also get a thumbs up via: sys.stdout.write('\uf164') However, for: os.write(1, '\uf164'.encode('utf8')) I get 3 characters, one for each encoded byte. How do I print the thumbs up using os.write ? 来源: https://stackoverflow.com/questions/62502211/printing-a-font-icon-with-os-write

Printing a font icon with os.write

偶尔善良 提交于 2020-06-23 14:06:16
问题 With Python 3.8.3 on Windows 10, my terminal, Set with a nerdfont that has icons, shows the thumbs up icon for: import os, sys # For the following attempts print('\uf164') I also get a thumbs up via: sys.stdout.write('\uf164') However, for: os.write(1, '\uf164'.encode('utf8')) I get 3 characters, one for each encoded byte. How do I print the thumbs up using os.write ? 来源: https://stackoverflow.com/questions/62502211/printing-a-font-icon-with-os-write

How to close file descriptors in python?

风流意气都作罢 提交于 2020-06-16 03:39:38
问题 I have the following code in python: import os class suppress_stdout_stderr(object): ''' A context manager for doing a "deep suppression" of stdout and stderr in Python, i.e. will suppress all print, even if the print originates in a compiled C/Fortran sub-function. This will not suppress raised exceptions, since exceptions are printed to stderr just before a script exits, and after the context manager has exited (at least, I think that is why it lets exceptions through). ''' def __init__

Modifying “… | tee -a out.txt” to stream output live, rather than on completion?

 ̄綄美尐妖づ 提交于 2020-06-15 05:59:08
问题 I would need to output the output of a command on a file. Let's say my command is zip -r zip.zip directory , I would need to append/write (any of these options would be fine) to a file (let's say out.txt ). I got zip zip.zip directory | tee -a out.txt so far, but it doesn't seem to work, it just writes the whole output when the command is over... How can I achieve this? Thanks ;) 回答1: Background (ie. Why?) Redirections are immediate -- when you run somecommand | tee -a out.txt , somecommand