Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old March 30th, 2004, 01:48 AM
7imz 7imz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 10 7imz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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...

Reply With Quote
  #2  
Old March 30th, 2004, 02:56 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
< 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
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
  #3  
Old March 30th, 2004, 03:29 AM
7imz 7imz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 10 7imz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks a lot

Reply With Quote
  #4  
Old March 30th, 2004, 07:36 AM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > distinguishing between two forms of input


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway