这是密码。不知何故输出是不一致的。ip.txt中的前2行有一个新的行,而第三个行正在按预期工作。
code.py
import subprocess
with open('ip.txt') as f:
for IPAddr in f:
ping = subprocess.Popen(['ping','-c','1',IPAddr],stdout=f).wait()
if ping == 0:
print(f'{IPAddr} is up')
命令
python -c "print('hello')"
在Linux (bash)和Windows (cmd.exe)中成功地运行引号中的代码。
如何用换行符传递代码,如何使用python -c**?**
例子:两者都是
python -c "for i in range(10): if i % 2 == 0: print('hello')"
python -c "for i in range(10):\n if i % 2 == 0:\n print('hello')"
有没有办法忽略通过raw_input输入的数据中的换行符?我正在尝试使用raw_input输入从电子表格复制和粘贴的字符串列表。问题在于,换行符似乎会导致数据过早输入。所有的空格都将被剥离,因此在输入数据时删除换行符将是一个额外的好处。此数据需要通过终端提示符直接输入,而不是从文件中读取。
这就是我到目前为止所做的:
names = raw_input('Shoot me some names partner: ')
print 'What do you want to do?'
print '1 - format names for program
我在linux上执行一个命令,并将其作为python的输出,它是一个字符串。
Product Name version Appliance Model
============ ======= ===============
ACD LINUX 1234 1.0 5550
其中,产品名称为"ACD Linux 1234"版本,其余列的设备模式为5550....respectively。
我想为每个项目创建一个键值对。示例:
"productname:ACD Linux 1234",version:
假设我有一个字符串sample,它是
"ApacheBench (ab) is a benchmarking tool that can load test servers by sending an arbitrary number of concurrent requests. It was designed for testing Apache installations.
cURL is a command line tool to send and receive data using various protocols like HTTP, HTTPS, FTP, LD
对于python3.7.4和下一段代码,win10和ubuntu18.04有一些奇怪的行为
def tokenize(text, match=re.compile(b"([idel])|(\d+):|(-?\d+)").match):
i = 0
while i < len(text):
m = match(text, i)
s = m.group(m.lastindex)
i = m.end()
if m.lastindex == 2:
yield "s
我正在试着用python打开一个程序。我的目标是在python启动子进程之后让终端对我可用。 import subprocess
subprocess.run(['feh', 'foo.png']) # locks program until I close the image = no go
subprocess.Popen('feh', 'foo.png']) # works but breaks new line in terminal 在终端中,我必须运行 $ stty sane 需要修复,但我看不到我输入的内容...
当我在Linux中使用python regex时,我遇到了一个问题。目标字符串包含多行,如
This is a matched string_1.
This is a matched string_22.
Do not match this line.
我想做的是匹配"\n\n“之前的所有内容。我用过
deleteString = re.compile('[\s\S]+\n\n')
但它似乎不能在Linux中工作。
如何匹配双精度\n之前的字符串。
感谢您的回复。
当我在Linux中对一个文件(一个有几百万行的CSV文件)执行wc -l时,它会报告比这个Python代码显示的代码低一千多行的行(只需对文件中的行进行迭代)。原因是什么?
with open(csv) as csv_lines:
num_lines = 0
for line in csv_lines:
num_lines += 1
print(num_lines)
我曾经遇到过wc报告比上面少一个的情况,这在文件没有终止换行符的情况下是有意义的,因为看起来wc会计算完整的行(包括终止换行符),而这段代码只会计算任何行。但是,一千行以上的差异会是什么情况