How to read a line in the terminal with python [duplicate] - python

This question already has answers here:
Capture stdout from a script?
(11 answers)
Closed 3 years ago.
I'm trying to get a already written line in python 3, but I haven't found any function that can read a line from the terminal. It should work something like sys.stdout.read(), or sys.stdout.readline() but this function just throws an error.

If you mean to read from the user/a pipe, then simply use input.
However, from your comments it seems like you want to be able to read from what has already been printed.
To do this, you have a few options. If you don't actually want it to display on the terminal, and you only care about certain part of the output, then you can use contextlib.redirect_stdout and contextlib.redirect_stderr. You can combine this with io.StringIO to capture the output of your application to a string. This has been discussed in the question Capture stdout from a script in Python
However, if you want to have something which provides you both a means of printing to the terminal and giving you the lines, then you will need to implement your own type which inherits from io.TextIOBase or uses io.TextIOWrapper.

Do you mean something like this?
name = input("Enter a name: ")
print(name)

Related

Is it possible run a string in Python [duplicate]

This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 1 year ago.
Is it possible to run a String text?
Example:
str = "print(2+4)"
Something(str)
Output:
6
Basically turning a string into code, and then running it.
Use exec as it can dynamically execute code of python programs.
strr = "print(2+4)"
exec(strr)
>> 6
I will not recommend you to use exec because:
When you give your users the liberty to execute any piece of code with the Python exec() function, you give them a way to bend the rules.
What if you have access to the os module in your session and they borrow a command from that to run? Say you have imported os in your code.
Sure is, looks like your "Something" should be exec.
str = "print(2+4)"
exec(str)
Check out this previous question for more info:
How do I execute a string containing Python code in Python?

Python zero as file parameter in open function [duplicate]

This question already has answers here:
Integer File Descriptor "0" in open()
(3 answers)
Closed 1 year ago.
I'm currently puzzling around this piece of code.
What exactly does
open(0)
do?
I already looked up the docs or tried to find something in the internet but no clues.
The codesnippet where this code is used:
map(abs,map(int,open(0).read().split()))
Thanks ^
0 is the file descriptor associated with stdin (1 corresponds to stdout, 2 to stderr). open takes int file descriptors as an argument, not just paths, so passing 0 is legal. This is just a somewhat obscure way to create a file object bound to stdin without needing to import sys.
The flaw with it is that, when it is closed, it will close the file descriptor 0 (since closefd=False was not passed), so sys.stdin will be closed without realizing it (though it'll probably figure it out when if someone tries to use it).

Python Gives me empty output [duplicate]

This question already has answers here:
Running python script in terminal, nothing prints or shows up - why?
(2 answers)
Closed 2 years ago.
At first I thought PythonScriptor was the problem, but I tried print('Hello World') and the output was fine.When ever I copy and paste a code it just gives me an empty line. I am currently learning Subroutines in Python, but I completely don't get it.
def myFirstSubroutine():
for i in range(1,3):
print('This is a subroutine')
The code snippet you just showed us defined the function. To execute the function, now you need to call it.
>>> myFirstSubroutine()
This is a subroutine
This is a subroutine
Thanks CoryKramer for his right answer. You need to call the function myFirstSubroutine() to get the output.
Pay attention to that lowercase letter variable is not equal to capital letter variable in Python. That means you need to use exact the function name myFirstSubroutine().
As well as 'I copied what you gave me , it doesn't work.', it can work if you really copy it instead of spelling the 'myfirstsubroutine' by yourself.

Iteration Over Open Text File Multiple Times in Python [duplicate]

This question already has answers here:
Iterating on a file doesn't work the second time [duplicate]
(4 answers)
Closed 6 years ago.
Surely this has been asked before but I cannot find the question.
As I understand it, in Python (I'm using 3.3 but this is generic to both 2.x and 3.x) you cannot iterate multiple times over an open text file and that this is due to the cursor being moved to the end and does not return to the start on the next iterable loop. Therefore it does not behave like a more typical iterable.
I was wondering how to return the cursor to the start, or at least have two for loops in succession over an open file to be read.
Thanks.
so, you're looking to rewind the file to the start again:
if your file handle is called f do it like this:
f.seek(0)
this won't work on streams, serial ports, pipes, or network sockets: only on regular files.

use os.system('MyCommand') in the next command [duplicate]

This question already has answers here:
Equivalent of Bash Backticks in Python [duplicate]
(11 answers)
Closed 8 years ago.
As far as you know, we can use OS console commands, (For example dir,time and format in Windows) in Python programing using os.system('TheCommand') module. But this function return the state of Operation (0 for successful and 1 for failed).
I want to know if is there any way to use the output of the commands in the next commands? I mean (For example) I run os.system('dir') and save the list of directories in a variable!
This is fairly easy to do. Here I define the working directory and the edit time of a file as variables which I used later in my script.
#!/usr/bin/env python
PWD = os.getcwd()
edit_time=os.path.getmtime(file.txt)

Categories