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 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],)
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. -
import sys
-
-
if sys.stdin.isatty():
-
print >>sys.stderr, "expected input from stdin"
-
sys.exit(1)
-
-
sys.stdout.write(sys.stdin.read())
-
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: -
import sys
-
-
filename = raw_input("Enter a filename:")
-
-
try:
-
f = open(filename)
-
except IOError,e:
-
print >>sys.stderr, e.strerror, e.filename
-
sys.exit(e.errno)
-
-
#do stuff with f
-
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
by: vim |
last post by:
hello everybody
Plz tell the differance between binary file and ascii
file...............
Thanks
in advance
vim
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
|
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...
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
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...
| |