|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
< 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 *** |
|
#3
|
|||
|
|||
|
thanks a lot
![]() |
|
#4
|
|||
|
|||
|
Quote:
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > distinguishing between two forms of input |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|