#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    10
    Rep Power
    0

    distinguishing between two forms of input


    how can i distinguish between two form of input such as stdin and command line

    python p.py < file.txt
    python p.py file.txt

    i tried if/else statements based on the presence of < in sys.argv but it doesn't seem to be detecting <... moreover, i dont' know want to do it based on the number of arguments (1 argument for stdin and 2 arguments for command line) as i may do it this way

    cat file.txt | python p.py

    i also may include other option after

    python p.py < file.txt -d whatever -1 wohoo

    any ideas?

    and one more question...

    how can output a file to a directory other than the current directroy... i'm pretty it is in th os module but i can't seem to find it...

    file = open("input.txt", "w")
    file.write("line")

    how can i output input.txt to another directory other than my current working directory...
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    < file.txt will not appear as a command line item because it is a directive to the shell. It is telling the shell to send the contents of the file to stdin of the program p.py.

    I suggest you do in fact check for a filename being provided (it will be argv[1] in your example). Or you could include a command line option for your program to read from stdin (e.g.
    python p.py --stdin). Have a look at the getopts module.

    To write to a file not in you current directory you can specify the absolute or relative path in the file command:
    file("/home/my_home/input.txt","w")
    file("./input.txt","w")

    This path format works on Windows too because Python will automatically convert the '/' to '\' for you

    Grim
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    10
    Rep Power
    0
    thanks a lot
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Originally Posted by Grim Archon
    This path format works on Windows too because Python will automatically convert the '/' to '\' for you
    I don't think it's the case that it converts it, just that Windows handles either slash as a path separator.

    Also, optparse is a more advanced module than getopt (note that it doesn't have an s on the end) and generally nicer to work with, IMO.
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.

IMN logo majestic logo threadwatch logo seochat tools logo