我如何拥有a)可以接受用户输入的Python脚本以及如何使用b)如果从命令行运行,则读入参数?
在Python中,如何检查是否sys.stdin有数据?
我发现不仅os.isatty(0)可以检查stdin是否连接到TTY设备,还可以检查是否有可用的数据.
但如果有人使用像
sys.stdin = cStringIO.StringIO("ddd")
Run Code Online (Sandbox Code Playgroud)
在使用之后os.isatty(0),它仍然返回True.我需要做些什么来检查stdin是否有数据?
我写了一个脚本,我希望它可以在bash中管道化.就像是:
echo "1stArg" | myscript.py
Run Code Online (Sandbox Code Playgroud)
可能吗?怎么样?
是否有单行程序来读取Python中文件的所有行,而不是标准:
f = open('x.txt')
cts = f.read()
f.close()
Run Code Online (Sandbox Code Playgroud)
似乎经常这样做,所以必须有一个单行.有任何想法吗?
是否可以先运行程序,然后在命令行中等待用户的输入.例如
Run...
Process...
Input from the user(in command line form)...
Process...
Run Code Online (Sandbox Code Playgroud) 我想运行一个mysql命令,并将其输出设置为我的python脚本中的变量.
这是我正在尝试运行的shell命令:
$ mysql my_database --html -e "select * from limbs" | ./script.py
Run Code Online (Sandbox Code Playgroud)
这是python脚本:
#!/usr/bin/env python
import sys
def hello(variable):
print variable
Run Code Online (Sandbox Code Playgroud)
我如何接受python脚本中的变量并让它打印输出?
当管道来自"打开"(不知道正确的名称)文件时,我有问题从python中的标准输入或管道读取.
我有例如 pipetest.py:
import sys
import time
k = 0
try:
for line in sys.stdin:
k = k + 1
print line
except KeyboardInterrupt:
sys.stdout.flush()
pass
print k
Run Code Online (Sandbox Code Playgroud)
我运行了一段时间后继续输出和Ctrl + c的程序
$ ping 127.0.0.1 | python pipetest.py
^C0
Run Code Online (Sandbox Code Playgroud)
我没有输出.但如果我通过一个普通的文件,它的工作原理.
$ ping 127.0.0.1 > testfile.txt
Run Code Online (Sandbox Code Playgroud)
一段时间后,这将以Ctrl + c结束
$ cat testfile.txt | python pipetest.py
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.017 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.015 ms
64 bytes from 127.0.0.1: …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个使用python的多处理模块的脚本.脚本(让我们称之为myscript.py)将从另一个带有管道的脚本获取输入.
假设我像这样调用脚本;
$ python writer.py | python myscript.py
Run Code Online (Sandbox Code Playgroud)
这是代码;
// writer.py
import time, sys
def main():
while True:
print "test"
sys.stdout.flush()
time.sleep(1)
main()
//myscript.py
def get_input():
while True:
text = sys.stdin.readline()
print "hello " + text
time.sleep(3)
if __name__ == '__main__':
p1 = Process(target=get_input, args=())
p1.start()
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,因为sys.stdin对象对于主进程和p1是不同的.所以我试过这个解决它,
//myscript.py
def get_input(temp):
while True:
text = temp.readline()
print "hello " + text
time.sleep(3)
if __name__ == '__main__':
p1 = Process(target=get_input, args=(sys.stdin,))
p1.start()
Run Code Online (Sandbox Code Playgroud)
但我遇到了这个错误;
Process Process-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/multiprocessing/process.py", …Run Code Online (Sandbox Code Playgroud) 我需要从一些文本文件中选择一些数字.我可以用grep挑选出我需要的行,但不知道如何从行中提取数字.一位同事告诉我如何使用perl从bash执行此操作:
cat results.txt | perl -pe 's/.+(\d\.\d+)\.\n/\1 /'
Run Code Online (Sandbox Code Playgroud)
但是,我通常使用Python编写代码,而不是Perl代码.所以我的问题是,我可以用同样的方式使用Python吗?也就是说,我可以将bash中的内容传递给Python,然后将结果直接输入到stdout吗?......如果这是有道理的.或者Perl在这种情况下更方便吗?
我想编写一个读取stdin(无缓冲)的程序,并编写stdout(unbuffered)进行一些简单的char-by-char转换.为了举例说明,我想要x从stdin中删除所有字符.
python ×10
pipe ×3
stdin ×3
input ×2
unix ×2
bash ×1
command-line ×1
file-io ×1
filter ×1
python-2.7 ×1
redirect ×1
stdout ×1
user-input ×1