473,383 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

No file from stdin



Hi,
suppose a script of python is waiting for a file from the stdin and none
is given. How can I make the script to stop and, for example, print an
error message?

Sorry for the n00b question and thanks
May 24 '07 #1
3 1661
On 24 mai, 18:48, Tartifola <tartif...@gmail.comwrote:
Hi,
suppose a script of python is waiting for a file from the stdin and none
is given. How can I make the script to stop and, for example, print an
error message?

Sorry for the n00b question and thanks
import sys
import os.path

if len(sys.argv)<2:
print >>sys.stderr, 'Usage: %s filename' %
(os.path.basename(sys.argv[0]),)
sys.exit(1)

print 'Your filename is %s' % (sys.argv[1],)
May 24 '07 #2
On May 24, 9:48 am, Tartifola <tartif...@gmail.comwrote:
Hi,
suppose a script of python is waiting for a file from the stdin and none
is given. How can I make the script to stop and, for example, print an
error message?

Sorry for the n00b question and thanks
I'm not exactly sure what you mean. This assumes that you intended the
contents of the file be piped in from the command line.
Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. if sys.stdin.isatty():
  4. print >>sys.stderr, "expected input from stdin"
  5. sys.exit(1)
  6.  
  7. sys.stdout.write(sys.stdin.read())
  8.  
If you simply want a prompt "Enter a filename:", followed by an error
if it is not a valid file, I would use something like this:

Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. filename = raw_input("Enter a filename:")
  4.  
  5. try:
  6. f = open(filename)
  7. except IOError,e:
  8. print >>sys.stderr, e.strerror, e.filename
  9. sys.exit(e.errno)
  10.  
  11. #do stuff with f
  12.  


May 24 '07 #3

Hi,

On May 24, 9:48 am, Tartifola <tartif...@gmail.comwrote:
Hi,
suppose a script of python is waiting for a file from the stdin and none
is given. How can I make the script to stop and, for example, print an
error message?

Sorry for the n00b question and thanks

I'm not exactly sure what you mean. This assumes that you intended the
contents of the file be piped in from the command line.
[code]
import sys

if sys.stdin.isatty():
print >>sys.stderr, "expected input from stdin"
sys.exit(1)
That's exactly what I was looking for. I did not know about isatty()
thanks
May 25 '07 #4

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

Similar topics

2
by: SunRise | last post by:
Hi I am creating a C Program , to extract only-Printable-characters from a file ( any type of file) and display them. OS: Windows-XP Ple help me to fix the Errors & Warnings and explain...
2
by: Tiger | last post by:
I try to write a struct into a brut file but I can't write all var in this struct when I try to add user I have that : > testmachine:/usr/share/my_passwd# ./my_passwd -a users.db > Ajout d'une...
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
13
by: Nick Maclaren | last post by:
Currently logins are disabled to sourceforge.net, and (despite the comments) it won't let me report a bug anonymously. Does anyone know whether this is short or long term? I have a bug in 2.5 to...
5
by: Cameron Laird | last post by:
Question: import subprocess, StringIO input = StringIO.StringIO("abcdefgh\nabc\n") # I don't know of a compact, evocative, and # cross-platform way to exhibit this behavior. # For now, depend...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
9
by: Justme | last post by:
Novice programmer needs help with using fgets to read and ignore the first two lines of a file. I've gone thru the previous posting regarding fgets, but none of them seems to help my situation. I...
4
by: DyslexicAnaboko | last post by:
Hello, I have a module that is part of larger project that is giving me trouble, so I setup an example. Brief ===== I simply want to open a text file and make the contents avaliable...
3
by: Paddy | last post by:
Hi, I am am falling at the first hurdle when trying to access a library using ctypes. I have a file libucdb.so which the file command says is shared object, but I cannot get it to load: Any...
1
by: Dog Walker | last post by:
I need to call a function in a shared object with this signature: init_dialog(FILE *input, FILE *output) The FILE*'s are to stdin and stdout. The call from python is libdialog.init_dialog( x, y)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.