use lines as argument to a command

Discussion in 'Python' started by Shoryuken, Oct 15, 2007.

  1. Shoryuken

    Shoryuken Guest

    I'm new to Python, so my question may sounds naive. Here is it.

    I have a text file like this:

    www.a.com
    www.b.com
    www.c.com
    ....

    I want to read one line from this file at a time, which I know how to
    do. And use it as an argument to a command, for example, telnet www.a.com
    and so on. However I have no idea how to do this task.

    My purpose is to generate a brief report.

    Thank you in advance.
     
    Shoryuken, Oct 15, 2007
    #1
    1. Advertising

  2. Shoryuken

    Amit Khemka Guest

    On 10/15/07, Shoryuken <> wrote:
    > I'm new to Python, so my question may sounds naive. Here is it.
    >
    > I have a text file like this:
    >
    > www.a.com
    > www.b.com
    > www.c.com
    > ...
    >
    > I want to read one line from this file at a time, which I know how to
    > do. And use it as an argument to a command, for example, telnet www.a.com
    > and so on. However I have no idea how to do this task.


    I guess that you want to loop over a file and for each line in the
    file you want to call some
    external program with the line as the argument.

    Have a look at subprocess module
    http://docs.python.org/lib/module-subprocess.html

    If there is some else that you meant, please specify .

    Cheers,

    --
    --
    Amit Khemka
     
    Amit Khemka, Oct 15, 2007
    #2
    1. Advertising

  3. Shoryuken

    Shoryuken Guest

    > I guess that you want to loop over a file and for each line in the
    > file you want to call some
    > external program with the line as the argument.
    >
    > Have a look at subprocess module
    > http://docs.python.org/lib/module-subprocess.html
    >
    > If there is some else that you meant, please specify .
    >
    > Cheers,
    >
    > --
    > --
    > Amit Khemka


    Thanks alot, its working now. But I have another question (sorry I'm
    so noob), how can I redirect a command's output to a file (just like
    executing a linux command "dmesg>>stat.txt")?
     
    Shoryuken, Oct 16, 2007
    #3
  4. Shoryuken

    Amit Khemka Guest

    On 10/16/07, Shoryuken <> wrote:
    > > I guess that you want to loop over a file and for each line in the
    > > file you want to call some
    > > external program with the line as the argument.
    > >
    > > Have a look at subprocess module
    > > http://docs.python.org/lib/module-subprocess.html
    > >
    > > If there is some else that you meant, please specify .
    > >

    > Thanks alot, its working now. But I have another question (sorry I'm
    > so noob), how can I redirect a command's output to a file (just like
    > executing a linux command "dmesg>>stat.txt")?


    Since subprocess module allow you to call programs through (linux)
    shell , you can use the same syntax in your command.

    example:

    >>> from subprocess import call
    >>> call("echo foo >> bar.txt", shell=True)


    Cheers,

    --
    --
    Amit Khemka
     
    Amit Khemka, Oct 16, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack
    Replies:
    9
    Views:
    2,728
  2. Joe Wright
    Replies:
    0
    Views:
    545
    Joe Wright
    Jul 27, 2003
  3. lovecreatesbeauty

    How to know two lines are a pare parallel lines

    lovecreatesbeauty, Apr 27, 2006, in forum: C Programming
    Replies:
    11
    Views:
    686
    Old Wolf
    Apr 28, 2006
  4. Replies:
    1
    Views:
    476
    Jonathan Mcdougall
    Dec 6, 2005
  5. ±è ÁØ¿µ
    Replies:
    7
    Views:
    266
    Einar Magnús Boson
    Nov 27, 2008
Loading...

Share This Page