473,769 Members | 5,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to use subprocess.Pope n execute "find" in windows

In cmd, I can use find like this.

C:\>netstat -an | find "445"
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*

C:\>

And os.system is OK.
>>import os
os.system('ne tstat -an | find "445"')
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*
0
>>>
But I don't know how to use subprocess.Pope n to do this.

from subprocess import Popen, PIPE

p1 = Popen(['netstat', '-an'], stdout = PIPE)
p2 = Popen(['find', '"445"'], stdin = p1.stdout, stdout = PIPE)
print p2.stdout.read( )

It doesn't work.
Because subprocess.Pope n execute "find" like this.

C:\>find \"445\"
拒绝访问 - \

C:\>

It adds a '\' before each '"'.
How to remove the '\'?
Thank you.
Jun 27 '08 #1
8 4214
cl*****@gmail.c om wrote:
In cmd, I can use find like this.

C:\>netstat -an | find "445"
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*

C:\>

And os.system is OK.
>>>import os
os.system('n etstat -an | find "445"')
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*
0
>>>>

But I don't know how to use subprocess.Pope n to do this.
While there certainly are valid usecases for piping with subprocess in
Python - this ain't one I'd say. Just read the output of netstat yourself,
and filter for lines that contain the desired pattern.

Diez
Jun 27 '08 #2
On May 6, 11:19 am, clyf...@gmail.c om wrote:
In cmd, I can use find like this.

C:\>netstat -an | find "445"
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*

C:\>

And os.system is OK.>>import os
>os.system('net stat -an | find "445"')

TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*
0

But I don't know how to use subprocess.Pope n to do this.

from subprocess import Popen, PIPE

p1 = Popen(['netstat', '-an'], stdout = PIPE)
p2 = Popen(['find', '"445"'], stdin = p1.stdout, stdout = PIPE)
print p2.stdout.read( )
I would say that, according to documentation, the following should
work:
print p2.communicate( )[0]
Philippe
Jun 27 '08 #3
On May 6, 5:19 pm, clyf...@gmail.c om wrote:
In cmd, I can use find like this.

C:\>netstat -an | find "445"
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*

C:\>

And os.system is OK.>>import os
>os.system('net stat -an | find "445"')

TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*
0

But I don't know how to use subprocess.Pope n to do this.

from subprocess import Popen, PIPE

p1 = Popen(['netstat', '-an'], stdout = PIPE)
p2 = Popen(['find', '"445"'], stdin = p1.stdout, stdout = PIPE)
print p2.stdout.read( )

It doesn't work.
Because subprocess.Pope n execute "find" like this.

C:\>find \"445\"
拒绝访问 - \

C:\>

It adds a '\' before each '"'.
How to remove the '\'?
Thank you.
cannot help with the backslashes but try findstr instead of find
Jun 27 '08 #4
On May 6, 7:19*pm, clyf...@gmail.c om wrote:
In cmd, I can use find like this.

C:\>netstat -an | find "445"
* TCP * *0.0.0.0:445 * * * * * *0.0.0.0:0 * * * * * * *LISTENING
* UDP * *0.0.0.0:445 * * * * * **:*

C:\>

And os.system is OK.>>import os
>os.system('net stat -an | find "445"')

* TCP * *0.0.0.0:445 * * * * * *0.0.0.0:0 * * * * * * *LISTENING
* UDP * *0.0.0.0:445 * * * * * **:*
0

But I don't know how to use subprocess.Pope n to do this.

from subprocess import Popen, PIPE

p1 = Popen(['netstat', '-an'], stdout = PIPE)
p2 = Popen(['find', *'"445"'], stdin = p1.stdout, stdout = PIPE)
print p2.stdout.read( )
Get rid of the extra quotes. ie:
p2 = Popen(['find', '445'], stdin = p1.stdout, stdout = PIPE)

The quotes on the command line and on the os.system call are consumed
by the shell. The program doesn't see them.
Jun 27 '08 #5
On 5月7日, 上午9时45分, Justin Ezequiel <justin.mailing li...@gmail.com >
wrote:
On May 6, 5:19 pm, clyf...@gmail.c om wrote:
In cmd, I can use find like this.
C:\>netstat -an | find "445"
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*
C:\>
And os.system is OK.>>import os
>>os.system('ne tstat -an | find "445"')
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*
0
But I don't know how to use subprocess.Pope n to do this.
from subprocess import Popen, PIPE
p1 = Popen(['netstat', '-an'], stdout = PIPE)
p2 = Popen(['find', '"445"'], stdin = p1.stdout, stdout = PIPE)
print p2.stdout.read( )
It doesn't work.
Because subprocess.Pope n execute "find" like this.
C:\>find \"445\"
拒绝访问 - \
C:\>
It adds a '\' before each '"'.
How to remove the '\'?
Thank you.

cannot help with the backslashes but try findstr instead of find
Thank you.
findstr doesn't need quotes, so it works.
Jun 27 '08 #6
On 5月7日, 下午2时41分, alito <alito...@gmail .comwrote:
On May 6, 7:19 pm, clyf...@gmail.c om wrote:
In cmd, I can use find like this.
C:\>netstat -an | find "445"
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*
C:\>
And os.system is OK.>>import os
>>os.system('ne tstat -an | find "445"')
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *:*
0
But I don't know how to use subprocess.Pope n to do this.
from subprocess import Popen, PIPE
p1 = Popen(['netstat', '-an'], stdout = PIPE)
p2 = Popen(['find', '"445"'], stdin = p1.stdout, stdout = PIPE)
print p2.stdout.read( )

Get rid of the extra quotes. ie:
p2 = Popen(['find', '445'], stdin = p1.stdout, stdout = PIPE)

The quotes on the command line and on the os.system call are consumed
by the shell. The program doesn't see them.
You must be a linux user:)

I guess, in windows, the quotes are consumed by the c runtime library.
Mayby the "find" in windows doesn't use the argc/argv but the windows
API GetCommandLine( ).

I wrote a c program to prove it.

#include <windows.h>
#include <stdio.h>

int main(int argc, char **argv) {
int i;

printf("%s\n", GetCommandLine( ));
for (i = 0; i < argc; ++i)
printf("%d: %s\n", i, argv[i]);

return 0;
}

The output is:
C:\>test 1 2 "3"
test 1 2 "3"
0: test
1: 1
2: 2
3: 3

C:\>

Notice that, GetCommandLine( ) does not consume the quotes, but the
(char **argv) does.
Jun 27 '08 #7
En Wed, 07 May 2008 23:29:58 -0300, <cl*****@gmail. comescribi贸:
On 5鏈7鏃, 涓婂崍9鏃45鍒 , Justin Ezequiel <justin.mailing li...@gmail.com >
wrote:
>On May 6, 5:19 pm, clyf...@gmail.c om wrote:
p1 = Popen(['netstat', '-an'], stdout = PIPE)
p2 = Popen(['find', '"445"'], stdin = p1.stdout, stdout = PIPE)
print p2.stdout.read( )
It doesn't work.
Because subprocess.Pope n execute "find" like this.
C:\>find \"445\"
It adds a '\' before each '"'.
How to remove the '\'?
Thank you.

cannot help with the backslashes but try findstr instead of find

Thank you.
findstr doesn't need quotes, so it works.
Build the command line yourself -instead of using a list of arguments-. Popen doesn't play with the quotes in that case:

p1 = Popen(['netstat', '-an'], stdout = PIPE) # using list
p2 = Popen('find "445"', stdin = p1.stdout, stdout = PIPE) # using str
print p2.communicate( )[0]

--
Gabriel Genellina

Jun 27 '08 #8
On 5月8日, 下午5时39分, "Gabriel Genellina" <gagsl-....@yahoo.com. arwrote:
En Wed, 07 May 2008 23:29:58 -0300, <clyf...@gmail. comescribió:
On 5月7日, 上午9时45分, Justin Ezequiel <justin.mailing li...@gmail.com >
wrote:
On May 6, 5:19 pm, clyf...@gmail.c om wrote:
p1 = Popen(['netstat', '-an'], stdout = PIPE)
p2 = Popen(['find', '"445"'], stdin = p1.stdout, stdout = PIPE)
print p2.stdout.read( )
It doesn't work.
Because subprocess.Pope n execute "find" like this.
C:\>find \"445\"
It adds a '\' before each '"'.
How to remove the '\'?
Thank you.
cannot help with the backslashes but try findstr instead of find
Thank you.
findstr doesn't need quotes, so it works.

Build the command line yourself -instead of using a list of arguments-. Popen doesn't play with the quotes in that case:

p1 = Popen(['netstat', '-an'], stdout = PIPE) # using list
p2 = Popen('find "445"', stdin = p1.stdout, stdout = PIPE) # using str
print p2.communicate( )[0]

--
Gabriel Genellina
Thanks very much.
You solved my problem.
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2066
by: Galsaba | last post by:
I am trying to find a script to a "dealer locator" or "find a store near you". I will have two tables. One will include all US and Canada ZIPs including lat and longtitudes. The other one is a table of our dealers (or store) including thier ZIP codes. When the user enters his ZIP code, the application needs to check (based on the lat and long) if there is a dealer in a 30 miles radious. After the application finds the list of all the...
3
4144
by: Dmitry Jouravlev | last post by:
Hi, I have a number of C++ solutions in Visual Studio .NET and when i compile them using "Whole Program Optimization", certain projects report a LNK1171 error saying that c2.dll could not be loaded. The error contains the correct path to c2.dll (and it is definately there). This only happens on some projects and only when "whole program optimization" option is turned on. If i turn off this option, the problem goes away. I have other...
1
1826
by: Bart Masschelein | last post by:
Hello, I have a VC++ project which uses files located on my Unix account Browsing (eg. go to Definition etc) works fine, however, if I use "Fin in Files" it does not find any single instance of the string entered. guess? it is a Windows (find)/Unix (grep) problem, but is there also solution to it? That would be great. Thanks,
2
7665
by: Lenonardo | last post by:
Hi. I'm writing a VB.Net application to update multiple Excel Worksheets. I'm using late binding (i.e. all variables are objects + use createobject) I develop the application on an XP machine with Excel 2002 (version 10.0) installed - and the code works fine. I then test the code on a laptop running Excel 2000 (version 9.0) and it
2
1570
by: Bill nguyen | last post by:
How can I get a listbox to display the most closely matched items when user types in the first few letters/digits of displaymember value? Thanks a million Bill
0
2798
by: Rave | last post by:
This is a long shot, but I thought I'd try it. I am currently using excel as an inventory tool. I currently have a hand-held scanner plugged into a laptop for reading barcodes. Using the "Find and Replace" fuction, I scan the merchandise which then searches the spreadsheet for the matching inventory number. When it is found, I highlight that cell in yellow. After scanning and coloring all of my inventory I can then see exactly what is...
0
1240
by: zensunni | last post by:
The script I've made checks for identical fields in the database before it inserts a new row. However, there is one number that, when checked for, will not be found. It assumes there is no row, so it will insert it again....thus allowing for duplicate records. I've tried tonnes of data with no problems. But if I use this number, it skips it.Anyone have any clue? The number is "49.0350". Here's my code:...
1
2301
by: vps | last post by:
I have Apache 2.2 server running on Windows XP SP2 with PHP 5.2.5. I have run go-pear.bat in PHP directory (system-wide, all defaults). Running 損ear command lists additional commands. I cannot find 搈ail.php on local drives (performed a Windows search). Using 搈ail.php from separate PEAR package does not solve the problem, since "mail" class is not recognized then.
1
20762
by: sidd4one | last post by:
Hi Hi friends when ever i start my system i use get the one message as windows can not find "CSRCS.EXE".i tried everthing to delete but i can't.But friends i known its vires.please help in this and how to remove it. wating for replay sidd
0
9586
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we抣l explore What is ONU, What Is Router, ONU & Router抯 main usage, and What is the difference between ONU and Router. Let抯 take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10043
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.